使用LINQ进行分组 - 返回零记录

时间:2014-06-09 10:25:21

标签: c# .net linq

我目前正在学习LINQ等等,但我遇到了分组问题。我只想在组合框中为TxtProductFamily显示唯一值。我在下拉列表中输入零:

var sel = (from o in db.r2_machine group o by o.product_family).ToList();
TxtProductFamily.ItemsSource = sel;

3 个答案:

答案 0 :(得分:2)

你必须这样做:

var sel = (from o in db.r2_machine 
           group o by o.product_family into g
           select g.Key).ToList();
TxtProductFamily.ItemsSource = sel;

答案 1 :(得分:1)

分组后您没有选择任何数据。使用Select方法。

  var sel = (from o in db.r2_machine 
             group o by o.product_family into g 
             select g.Key).ToList();

  TxtProductFamily.ItemsSource = sel;

答案 2 :(得分:1)

您必须选择要获取输出的内容

var outPut= (from o in db.r2_machine 
           group o by o.product_family into g
           select g).ToList();
TxtProductFamily.ItemsSource = outPut;