IEnumerable来自IEnumerable>其中T有两个

我有一个IEnumerable>s,其中CustomObject有一个x(它像键一样使用(在本例中为1,2,3))和一个y值。一些假装数据:

{{{1,2},{2,4},{3,6},{4,8}},{{1,2},{2,0},{3,0},{4,-2}},{{1,2},{2,2},{3,0},{4,0}}}检索以下IEnumerable的最佳方法是什么:

{{1,2},{2,2},{3,2},{4,2}}

即每个元素的y值的平均值。

性能需要合理,因此不能使用.ToList()或类似功能。我一直在用LINQ尝试各种事情,但无济于事。

@Bort,@Rawling,我已经测试了你的答案,@Rawling的答案稍微快一点。然而,@Bort的答案更具可读性,所以我想我暂时会同意这一点。请随时提供答案!

使用LINQ,您可以使用SelectMany来展平列表列表,然后GroupByx并Select平均值:

varaverages=customObjectLists.SelectMany(l=>l).GroupBy(co=>co.x).Select(g=>newCustomObject{x=>g.Key,y=g.Average(co=>co.y)});这样的东西应该能让你得到你想要的结果。它会将列表列表平展为单个List,然后按X值分组并平均Y值,从而为您提供具有X和Y属性的匿名类型的IEnumerable。你可以更改selectnew{}...来调用CustomObject的构造函数,你会得到一个IEnumerable

varmyComplexObject=//yourIEnumerable>varresult=fromfirstListinmyComplexObjectfromsecondListinfirstListgroupsecondListbysecondList.Xintogrpselectnew{X=grp.Key,Y=(int)grp.Average(p=>p.Y)};如果您不介意固化外部枚举器,则以下LINQy方法将延迟内部枚举器的执行。

IEnumerableAggregateAcross(IEnumerable>input,Funcselect,Func,V>aggregate){varenumerators=input.Select(ie=>ie.GetEnumerator()).ToArray();while(enumerators.All(e=>e.MoveNext())){yieldreturnaggregate(enumerators.Select(e=>select(e.Current)));}}呼叫例如

foreach(varavginAggregateAcross(input,pair=>pair.y,e=>e.Average(y=>y))){Console.WriteLine(avg);}请注意,一旦其中一个内部枚举器用完元素,此操作就会停止。此外,它需要一些东西来在完成后释放所有枚举器。看看这个答案,了解更多的想法。

(另请注意,这完全忽略了x值。由于所有输入都井井有条,并且所需的输出也井井有条,因此x值不会添加任何内容。

THE END
1.C#IEnumerable.Where方法代码示例本文整理汇总了C#中IEnumerable.Where方法的典型用法代码示例。如果您正苦于以下问题:C# IEnumerable.Where方法的具体用法?C# IEnumerable.Where怎么用?C# IEnumerable.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnumerable的用法示例。 https://vimsky.com/examples/detail/csharp-ex---IEnumerable-Where-method.html
2.(转)IEnumerable和IEnumerator详解namespaceMyCarIEnumerator{publicclassGarage:IEnumerable{Car[]carArray=newCar[4];//启动时填充一些Car对象publicGarage(){carArray[0]=newCar("Rusty",30);carArray[1]=newCar("Clunker",50);carArray[2]=newCar("Zippy",30);carArray[3]=newCar("Fred",45);}publicIEnumeratorGetEnumerator(){return...https://www.jianshu.com/p/7943bbd826d7
3.如何批量遍历IEnumerable然后可以在方法中调用它。public IEnumerable<user> GetBatch(int pageNumber){    return ...https://m.imooc.com/wenda/detail/603656
1.IEnumerable介面(System.Collections)MicrosoftLearnCast<TResult>(IEnumerable) 將IEnumerable 的項目轉換成指定的型別。 OfType<TResult>(IEnumerable) 根據指定的型別篩選 IEnumerable 的專案。 AsParallel(IEnumerable) 啟用查詢的平行處理。 AsQueryable(IEnumerable) 將IEnumerable 轉換成 IQueryable。 適用於 產品版本 .NET Core 1.0, Core 1.1, Core 2.0, Core...https://msdn.microsoft.com/zh-tw/library/h1x9x1b1(v=vs.90)
2.C#IEnumerator,IEnumerable,Iteratorienumerable<int>C# IEnumerator,IEnumerable ,Iterator IEnumerator 枚举器接口 在C#语言中,大部分以“I”字母开头命名的都是接口,所以情理之中,IEnumerator也是一个接口。 对于面向对象语言来说,接口就是一份“协议”,它定义了一组方法、属性和事件的契约,任何类、结构体或枚举只要符合这个契约,就可以被认为实现了该接口,可以被贴...https://blog.csdn.net/jshxjd/article/details/143665403
3.C#中IEnumerableICollectionIListList之间的区别C#教程按照功能排序:List<T> 《IList<T> 《ICollection<T>《IEnumerable<T> 按照性能排序:IEnumerable<T>《ICollection<T>《IList<T>《List<T> 另一种解释: ICollection 接口是 System.Collections 命名空间中类的基接口,ICollection 接口扩展 IEnumerable,IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。如果...https://www.jb51.net/article/218178.htm
4.C#中IEumerable的简单了解51CTO博客一、IEnumerable简单介绍 IEnumerable是可枚举类型,一般在迭代时应用广泛,如foreach中要循环访问的集合或数组都实现了IEnumerable接口。只要能够遍历,都直接或间接实现了IEnumerable接口。如:String类型的对象,可遍历,输出时以字符输出,间接实现了IEnumerable接口,"OOP"遍历打印就是'O','O','P';又如int类型没有实现IEnum...https://blog.51cto.com/u_4018548/6420389
5.StephenWaltheronASP.NETMVC6:IEnumerable GetCustomers(); 7: 8:} The IData interface has one method named GetCustomers() that, when implemented, should return a list of customers. Here’s a quick sample of how you can mock this interface with Typemock Isolator: ...https://weblogs.asp.net/stephenwalther/archive/2008/03/16/tdd-introduction-to-typemock-isolator.aspx
6....acommaseparatedlistfromIList<string>orIEnumerable<...What is the cleanest way to create a comma-separated list of string values from anIList<string>orIEnumerable<string>? String.Join(...)operates on astring[]so can be cumbersome to work with when types such asIList<string>orIEnumerable<string>cannot easily be converted into a string ...https://stackoverflow.com/questions/799446/creating-a-comma-separated-list-from-iliststring-or-ienumerablestring
7.DDD理论学习系列(12)仓储腾讯云开发者社区对于这种问题,我们最好在仓储中的方法中,比如List()或者ListActive()做默认处理,而不是在应用服务层每次去指定查询条件。 但具体是返回 IQueryable还是IEnumerable每个人的看法不一,具体可参考Repository 返回 IQueryable?还是 IEnumerable?。 5. 事务管理和工作单元...https://cloud.tencent.com/developer/article/1018485