Remove Duplicate based on column value-linq(根据列值-linq 删除重复项)
问题描述
我有员工和团队之间的多对多关系.以下 linq 语句
i have many to many relationship between employee and group. following linq statement
int[] GroupIDs = {6,7};
var result = from g in umGroups
join empGroup in umEmployeeGroups on g.GroupID equals empGroup.GroupID
where GroupIDs.Contains(g.GroupID)
select new { GrpId = g.GroupID,EmployeeID = empGroup.EmployeeID };
返回 groupid 和雇员 ID.结果是
returns groupid and the employeeid. and result is
GrpId | EmployeeID
6 | 18
6 | 20
7 | 19
7 | 20
我需要删除员工 ID 重复的行,例如雇员id = 20的行中的任何一项
谢谢
I need to remove the rows for which the employeeid is repeating e.g. any one of the row with employeeid= 20
Thanks
推荐答案
好吧,如果你不在乎解雇了哪位员工,你可以试试这样的:
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
方法:
At that point you could actually use MoreLINQ which has a handy DistinctBy
method:
var result = query.AsEnumerable()
.DistinctBy(x => x.EmployeeId);
这篇关于根据列值-linq 删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据列值-linq 删除重复项


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01