ienumerable 相关问题

IEnumerable,它的通用对应物IEnumerable <T>是用于通过项集合迭代(或枚举)的.NET接口。

将 3 个 IEnumerable<Application> 列表合并为一个

我简化了我的问题 公开课申请 { 公共 int Id { 得到;放; } 公共 int 版本 { 获取;放; } 公共 int 状态 { 获取;放; } } 我的 3 个清单: IE可枚举 我简化了我的问题 public class Application { public int Id { get; set; } public int Version { get; set; } public int Status { get; set; } } 我的 3 个清单: IEnumerable<Application> applications1 = new List<Application> { new Application {Id = 1, Version = 1, Status = 1}, new Application {Id = 2, Version = 1, Status = 1}, new Application {Id = 3, Version = 3, Status = 1} }; IEnumerable<Application> applications2 = new List<Application> { new Application {Id = 1, Version = 2, Status = 2}, new Application {Id = 2, Version = 2, Status = 2}, new Application {Id = 3, Version = 1, Status = 0} }; IEnumerable<Application> applications3 = new List<Application> { new Application {Id = 1, Version = 5, Status = 1}, new Application {Id = 2, Version = 0, Status = 1}, new Application {Id = 3, Version = 6, Status = 1} }; 我想制作这个: IEnumerable<Application> applications4 = new List<Application> { new Application {Id = 1, Version = 5, Status = 2}, new Application {Id = 2, Version = 2, Status = 2}, new Application {Id = 3, Version = 6, Status = 1} }; 即具有最高版本和状态值的列表。 我尝试过 LINQ 但我不明白。 我最好的是这个,但它只是获得了最高版本。我不明白当有 2 个属性可供选择时我该怎么做: IEnumerable<Application> applications4 = applications1 .Concat(applications2) .Concat(applications3) .GroupBy(a => a.Id) .Select(g => g.Aggregate((acc, curr) => acc.Version > curr.Version ? acc: curr )) .ToList(); 您应该使用 Max() 从分组属性中获取最大值。 IEnumerable<Application> applications4 = applications1 .Concat(applications2) .Concat(applications3) .GroupBy(a => a.Id) .Select(g => new Application { Id = g.Key, Version = g.Max(x => x.Version), Status = g.Max(x => x.Status) }) .ToList(); 您的问题忽略了一个非常重要的点 - 哪个属性(版本或状态)更重要? 我假设版本更重要,并创建了一个自定义比较器来对结果进行排序。基本上我想要的是按应用程序 ID 对项目进行group,然后获得最高的结果。我使用自定义比较器来获取它。如果您不想使用比较器,可以随时将其更改为: .Select(x => x.OrderByDescending(x => x.Version).ThenBy(x => x.Status).First()); internal class Program { static async Task Main(string[] args) { IEnumerable<Application> applications1 = new List<Application> { new Application {Id = 1, Version = 1, Status = 1}, new Application {Id = 2, Version = 1, Status = 1}, new Application {Id = 3, Version = 3, Status = 1} }; IEnumerable<Application> applications2 = new List<Application> { new Application {Id = 1, Version = 2, Status = 2}, new Application {Id = 2, Version = 2, Status = 2}, new Application {Id = 3, Version = 1, Status = 0} }; IEnumerable<Application> applications3 = new List<Application> { new Application {Id = 1, Version = 5, Status = 1}, new Application {Id = 2, Version = 0, Status = 1}, new Application {Id = 3, Version = 6, Status = 1} }; // you can use OrderByDescending().First() for applications using EF lower than 6.0.0 var applications4 = applications1.Union(applications2).Union(applications3) .GroupBy(x => x.Id) .Select(x => x.MaxBy(x => x, new VersionStatusComparer())); foreach (var item in applications4) { Console.WriteLine("Id: {0} Version: {1} Status: {2}", item.Id, item.Version, item.Status); } } } internal class VersionStatusComparer : IComparer<Application> { public int Compare(Application? x, Application? y) { var versionResult = CompareVersion(x.Version, y.Version); if (versionResult == 0) { return CompareStatus(x.Status, y.Status); } return versionResult; } private int CompareVersion(int xVersion, int yVersion) { if (xVersion > yVersion) { return 1; } else if (xVersion < yVersion) { return -1; } return 0; } private int CompareStatus(int xStatus, int yStatus) { if (xStatus > yStatus) { return 1; } else if (xStatus < yStatus) { return -1; } return 0; } } } 这是一种更面向 OOP 的方法,但我希望它看起来更明确。如果您需要更复杂的场景,您可以更新 Merge 逻辑。 它具有 O(n) 复杂度(n 个元素的数组需要 n 个“陡峭”才能完成)并使用字典: public class Application { public int Id { get; set; } public int Version { get; set; } public int Status { get; set; } public bool Merge(Application app){ if(app.Id != Id) return false; this.Version = Math.Max(app.Version, this.Version); this.Status = Math.Max(app.Status, this.Status); return true; } } //... IEnumerable<Application> applications4 = new List<Application>(); Dictionary<int,Application> res = new Dictionary<int,Application>(); foreach( var app in applications1.Concat(applications2).Concat(applications3) ){ // existing value, then update if( res.TryGetValue( app.Id, out Application appToProceed )) appToProceed.Merge(app); // new id value, then add else res.Add(app.Id,app); } // applications4 = res.Values.ToList(); 关于基准测试结果,它是关于 ns 的,所以只是作为信息: | Method | Mean | Error | StdDev | Gen0 | Allocated | |---------------------------------------------- |-----------:|--------:|--------:|-------:|----------:| | Benchmark_Using_Dictionary__3List_1000Element | 226.8 ns | 3.03 ns | 2.84 ns | 0.0780 | 408 B | | Benchmark_Using_LINQ__3List_1000Element | 299.7 ns | 4.04 ns | 3.59 ns | 0.1040 | 544 B | | Benchmark_Using_Dictionary__3List_3Element | 652.5 ns | 2.09 ns | 1.85 ns | 0.1125 | 592 B | | Benchmark_Using_LINQ__3List_3Element | 1,631.4 ns | 8.12 ns | 7.59 ns | 0.2842 | 1488 B | (3List 表示 app1 + app2 + app3 和 1000Element 表示每个应用程序有 1000 个 id 可以使用)

回答 3 投票 0

将 3 个 IEnumerable<customObj>“列表”合并为 1 个 IEnumerable<customObj>

我简化了我的问题 自定义对象 公开课申请 { 公共 int Id { 得到;放; } 公共 int 版本 { 获取;放; } 公共 int 状态 { 获取;放; } } 我的 3 个“列表&q...

回答 2 投票 0

嗨,如何在 IEnumerable<> 列表上使用 foreach?

public IEnumerable 合并(IEnumerable 第一,IEnumerable 第二) { }

回答 1 投票 0

如何测试 IEnumerable.GetEnumerator() 方法?

假设我有一个生成斐波那契数的类: 公共类斐波那契数列:IEnumerable { 公共 IEnumerator GetEnumerator() { ...

回答 4 投票 0

C#:如何测试 IEnumerable.GetEnumerator() 方法?

假设我有一个生成斐波那契数的类: 公共类斐波那契数列:IEnumerable { 公共 IEnumerator GetEnumerator() { ...

回答 4 投票 0

如何对使用存储过程检索的记录在 ToList() 之前执行 Count()?

这是我的代码: // 网格查询 var data = ctx.spRewards(月, 年, null, MedicalID).Select(p => new { MedicalID = p.MedicalID, 日期奖励 = p.日期奖励, 医疗 = p.医疗,

回答 2 投票 0

用Enumerable将多个集合组合成一个结构体集合?

我有多个对象数组: A、B、C。 它们都具有相同的长度。 我想编写一个函数,将数组的每个元素“合并”为结果数组中的单个元素。 一个

回答 1 投票 0

Parallel.ForEach 明显克隆引用类型?

我有一个简单的方法: 私有 IEnumerable 提交订单(IEnumerable 项目) { ConcurrentBag 结果 = 新 ConcurrentBag...

回答 1 投票 0

在我的 C# 代码中,当我尝试定义 IEnumerable 任务的返回时,出现编译器错误 CS0029。我该如何纠正?

在下面的 C# 代码中,定义 IEnumerable 任务的返回值时出现编译错误(代码的最后一行) 编译器错误错误 CS0029 无法将类型“void”隐式转换为“System”。

回答 1 投票 0

Enum 可以包含其他 Enum 吗?

Enum 可以包含其他 Enum 元素以及它自己的元素吗? 公共枚举 CardSuit { 黑桃, 心, 钻石, 俱乐部 } 公共枚举 GameSuit { 黑桃,// Th...

回答 5 投票 0

通用 NOT 约束,其中 T : !IEnumerable

根据标题,是否可以在 c# 4 中声明类型否定约束? 感兴趣的用例是允许以下重载共存 void doIt(T 什么){} ...

回答 7 投票 0

C# 角色提供者。 getallroles() 方法重写 Var userdetails= HttpContext.Current.GetOwinContext().Authentication.User.Claims;

我在登录页面中使用 SSO,虽然通过 okta 进行身份验证并接收用户声明详细信息,但代码无法正常工作,而是在现有角色提供程序类旁边进行控制。

回答 1 投票 0

LINQ 中的 .Where 实际上是如何工作的?

在 Joseph Albahari 的《C# in a Nutshell》一书中,据说 .Where 的实现如下: 公共静态 IEnumerable 其中 (此 IEnumerable 在 Joseph Albahari 的《C# in a Nutshell》一书中,据说 .Where 的实现如下: public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource,bool> predicate) { foreach (TSource element in source) if (predicate (element)) yield return element; } 但对我来说,这里使用“foreach”似乎会枚举我们的集合,所以在我们有 collection.Where(x => ...).Where(x => ...).ToList() 的情况下 -枚举将发生 3 次:第一次在第一个Where中,第二次在第二个Where中,最后一次在ToList()中。但我在想,LINQ 链的整个思想就是只枚举一次。 我是否弄错了,它只会被枚举一次?请指导我 看起来在这里使用“foreach”将枚举我们的集合, 它确实看起来像这样,但实际情况并非如此。相反,yield关键字告诉编译器应该对方法执行转换,以便它返回一个IEnumerable对象,该对象仅知道如何迭代集合,而无需实际执行该工作。在您实际使用并迭代此返回的 IEnumerable 之前,根本不会发生枚举。 编译器转换以可堆栈的方式发生,因此即使多次调用.Where()(如问题中的collection.Where(x => ...).Where(x => ...).ToList()表达式),仅发生一次枚举。您也可以混合使用其他 linq 操作,例如 .Select()、.Any()、.Aggregate() 等。 这可以使编写 linq 代码极其高效。缺点是每次使用此方法都会导致枚举对象的内存分配,因此了解内存受限与 CPU 受限的情况很有用,并且通常值得将其中一些结合起来,因此 .Where(x => ...).Where(x => ) 改为.Where(x => ... && ...)。 最后,虽然也有例外,但您应该 尽可能避免调用 ToList()(或 ToArray())。相反,对于方法参数、返回类型和变量声明,请使用 IEnumerable<T> 而不是 List<T> 或 T[]。在许多情况下,最终的foreach循环、数据绑定或其他机制足以强制最终枚举,这样您实际上不需要构造列表或数组,并且这些其他选项更有效 而不是实际构建一个可能不需要的列表。

回答 1 投票 0

支持 IEnumerable 时奇怪的 JSON 序列化

我在支持 IEnumerable 和 JSON 序列化时遇到了一个特殊问题。我在这里和网上搜索了一段时间,发现这个问题并不新鲜。然而,我发现的是

回答 0 投票 0

按包含列表索引和位置的元组对项目列表进行排序

我有一个物品清单 IEnumerable 来源 [0] 来源项目 0 [1] sourceItem1 [2] sourceItem2 [3] sourceItem3 .. 我想组织它/按我的元组列表排序 var order= 新 L...

回答 0 投票 0

如何将对象投射到匿名 T

我在使用 C# 时遇到问题 首先,我的辅助功能,以便更好地理解 公共静态类扩展{ public static IEnumerable ToEnumerable(这个 T 值...

回答 0 投票 0

使用返回类型 List<T> 与 IEnumerable<T>

我创建了一个函数 foo,它创建并返回一种 List。 ReSharper 建议我将返回类型更改为 IEnumerable。但是,我知道在函数中...

回答 4 投票 0

IQueryCollection.ToList() 不需要地组合值

我的项目使用了两个库: Swashbuckle.ASPNetCore 流利验证 我正在编写一个验证器拦截器来做一些额外的检查,这些检查需要与验证器分开:MyModelValida ...

回答 1 投票 0

IEnumerable First()、ElementAt(0) 和 Count() 在本地执行得非常慢 [关闭]

我在 IEnumerable 上执行了 Count() 以查看它返回了多少条记录。它们是大约 25 个属性的对象。我在代码中打了一个断点,差不多用了 2 分钟才返回。这...

回答 0 投票 0

何时将 IEnumerable 转换为 IAsyncEnumerable

在控制器操作返回类型的 .NET 文档(文档链接)中,它显示了有关如何返回异步响应流的示例: [HttpGet("异步销售")] 公共异步 IAsyncEnumerab ...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.