如何删除linq查询中的可重复项

时间:2012-11-13 04:41:23

标签: c#

listOrders.DataSource = (from sp in dbdata.Specifications
                         join ord in dbdata.Orders on sp.O_id equals ord.O_id
                         join prd in dbdata.Products on ord.O_id equals prd.O_ID
                         where sp.Approve == "Yes" && 
                         sp.Awailable_BOM == "Yes" && 
                         prd.Hours_prd == null
                         orderby sp.O_id descending
                         select sp.O_id).Distinct();

在这里我试图获得降序值。但它总是会提升价值。如果我删除“Distinct()”它可以正常工作但添加“Distinct()”后会出现此问题。

1 个答案:

答案 0 :(得分:1)

试试这个:

listOrders.DataSource = (from sp in dbdata.Specifications
                         join ord in dbdata.Orders on sp.O_id equals ord.O_id
                         join prd in dbdata.Products on ord.O_id equals prd.O_ID
                         where sp.Approve == "Yes" && 
                         sp.Awailable_BOM == "Yes" && 
                         prd.Hours_prd == null
                         select sp.O_id).Distinct().OrderByDescending();