c# - 使用 Linq 查询区分

标签 c# .net linq linq-to-entities

每个 InfringementEntity 都有一个类型。

foreach (InfringementEntity ie in _infCol.InfCollection.Select(r=>r).Distinct())
{
    InfringementLodgementEntity.InfringementCollection.InfCollection.Add(ie);
}

InfringementLodgementCollection.InfringementLodgementEntities
    .Add(InfringementLodgementEntity);

我需要选择所有类型不同的 Infringement Entity 并将它们插入到新的 InfringementLodgementEntity 中。然后在 InfringementLodgementCollection 中添加这个 InfringementLodgementEntity。

问题是我如何选择不同类型的 infringementEntity 将它们添加到新的 InfringementLodgementEntity 中。

最佳答案

你应该实现一个 IEqualityComparer<InfringementEntity> 检查类型,并使用 Distinct 接受这样一个比较器的重载。

关于c# - 使用 Linq 查询区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5218410/

相关文章:

c# - 你可以在 C# 中将函数分配给变量吗?

c# - 是否存在 TcpClient.Close 或 Socket.Close(0) 可能阻止我的代码的情况?

c#、mp3 和文件路径

c# - 如何在 LINQ select 语句中使用 Lambda

c# - 如何在 IEnumerable 对象中执行 foreach

c# - 如何获取控制台应用程序窗口的句柄

c# - Selenium 与 UWP 应用程序?

c# - 在 SQL Server 2008 中处理来自 c# 的多个数据库更新

.net - margin :0 auto; not working for the whole page

sql - .Net Core Entity Framework Linq Fluent API 查询不会创建/执行在数据库上运行的正确 SQL 查询