0

可能重复:
DISTINCT() 和 ORDERBY 问题

listOrders.DataSource = (from sp in dbdata.Specifications.Where(i => i.Approve == "Yes" && i.Awailable_BOM == "Yes")
                                     join ord in dbdata.Orders on sp.O_id equals ord.O_id
                                     join prd in dbdata.Products.Where(k => k.Hours_prd == null) on ord.O_id equals prd.O_ID
                                     orderby ord.Special, sp.Due_date
                                     select ord.O_id).Distinct();

我想按“ord.Special, sp.Due_date”订购,但在我输入“Distinct();”后它不起作用。那么如何按“ord.Special, sp.Due_date”排序并获得不同的值。

4

1 回答 1

0

我认为这将返回有序的 id:

listOrders.DataSource = 
   from sp in dbdata.Specifications.Where(i => i.Approve == "Yes" && i.Awailable_BOM == "Yes")
   join ord in dbdata.Orders on sp.O_id equals ord.O_id
   join prd in dbdata.Products.Where(k => k.Hours_prd == null) on ord.O_id equals prd.O_ID
   select new { ord.Special, sp.Due_date, ord.O_id } into x
   group x by x.O_id into g
   select new { g.Key, X = g.OrderBy(i => i.Special).ThenBy(i => i.Due_date).First()) } into y
   orderby y.X.Special, y.X.Due_date
   select y.Key;

注释:

  • 只选择您感兴趣的领域x
  • 按您想要区分的字段将这些字段分组g
  • 从每个组中选择键(ord.O_id)和组项X,这将定义结果中 id 的顺序(在这种情况下First,将返回 id 的出现)
  • 排序结果X
  • 选择Key( ord.O_id)
于 2012-11-23T06:56:02.460 回答