且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于列的值,LINQ删除重复

更新时间:2021-12-04 23:11:20

好吧,如果你不在乎哪个员工被删除,你可以尝试这样的:

Okay, if you don't care which employee is removed, you could try something like:

var result = query.GroupBy(x => x.EmployeeId)
                  .Select(group => group.First());

您还没有指定这是否是LINQ to SQL中的LINQ to Objects还是其他什么东西......我不知道这个SQL转换将是什么。如果你正在处理的数据量相对较少,你可以总是强制此最后一点是在工艺:

You haven't specified whether this is in LINQ to SQL, LINQ to Objects or something else... I don't know what the SQL translation of this would be. If you're dealing with a relatively small amount of data you could always force this last bit to be in-process:

var result = query.AsEnumerable()
                  .GroupBy(x => x.EmployeeId)
                  .Select(group => group.First());

在这一点上,你可以实际使用 MoreLINQ 它有一个方便的DistinctBy$c$c>方法:

At that point you could actually use MoreLINQ which has a handy DistinctBy method:

var result = query.AsEnumerable()
                  .DistinctBy(x => x.EmployeeId);