开发者

how to use parameter in linq to select different columns

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?


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜