C# Distinct在linq中的嵌套select中不工作
执行此查询时,Distinct()不起作用。它选择内部查询结果中的所有记录。如何修改此选项以在内部查询结果中获得不同的行。您可以使用group by进行此操作,下面的代码可以帮助您解决此问题C# Distinct在linq中的嵌套select中不工作,c#,linq,C#,Linq,执行此查询时,Distinct()不起作用。它选择内部查询结果中的所有记录。如何修改此选项以在内部查询结果中获得不同的行。您可以使用group by进行此操作,下面的代码可以帮助您解决此问题 (from SN1 in uow.SystemNotifications join sn2 in ( from SN2 in uow.SystemNotifications select new { SN2.UserId, SN2.Notific
(from SN1 in uow.SystemNotifications
join sn2 in
(
from SN2 in uow.SystemNotifications
select new { SN2.UserId, SN2.NotificationTypeId, SN2.ItemId }
).Distinct()
on new { SN1.UserId, SN1.NotificationTypeId }
equals new { sn2.UserId, sn2.NotificationTypeId }
select SN1).ToList();
如果您有任何问题,请告诉我。是什么类型的
UserId
、NotificationTypeId
和ItemId
?UserId、NotificationTypeId和ItemId是否与第一行相似?因为这里的distinct会找到三个不同的组合。UserId、NotificationTypeId是int而不是null,itemId是intnull@Iti-对,这三列重复多次,但与之对应的其他列具有不同的值。@Mahesh:是的,这就是原因,因为distinct在这里会找到不同的组合,它们可以是多个组合。使用分组方式。
var rootcategories2 = (from p in sr.products
group p.subcategory by p.category into subcats
select subcats);