tags:

views:

58

answers:

1

i want to use linq to select certain columns to populate each combobox. right i have individual linq query to do the job. i wish to write a method to do that.

var getUserName = Entity.Select(a=>a.Username);
var getType  = Entity.Select(a=>a.Type);
var getAddress  = Entity.Select(a=>a.Address);

can i do something like that:

   Public object GetData(string columnName) {  
 var q = from a in Entity
              Select columnName;
 return q.distinct();

    }

combobox1.bindingsource = GetData("Username");
combobox2.bindingsource = GetData("Type");
combobox3.bindingsource = GetData("Address");

do i need to write a construct?

A: 

Dynamic LINQ will be helpful in this situation.
Here is a tutorial.
The code you need will look like this:

    public object GetData (string colName) {
      NorthwindDataContext db = new NorthwindDataContext();
      var q = db.Products.Select(colName);
      List list = new List();
      foreach (var element in q) {
        if (!list.Contains(element))
          list.Add(element);
      }
      return list;
Devart