3 个 IEnumerable 的平均值
我需要平均 3 个 IEnumerable 集合并创建一个新的 IEnumerable,结果如下:
var result1 = GetResult1()//Returns an enumerable collection having elements 1,2,3,4
var result2 = GetResult2()//Returns an enumerable collection having elements 3,4,2,6
var result3 = GetResult3()//returns an enumerable collection having elements 2,5,1,6
//I need to create a collection which has the averages of the above results as below:
var result4 = GetResult4()//Should return 2.00,3.67,2.33,5.33.
每个结果将具有相同数量的元素,即 result1、result2、result3 将具有相同数量的元素(在本例中为 4) ,但实际上它的范围可以达到 1500)。
结果中的每个元素对应于结果的平均值,即在 result4 集合中,第一个元素将是 1,3,2 的平均值,分别对应于 result1,result2,result3 中的第一个元素的平均值
结果集合的小数位应四舍五入到 2。
一个明显的答案是迭代集合中的每个元素并在结果中创建元素手动收集(即通过 for 循环),但是我想避免它,因为结果将在每个集合中包含接近 1500 个元素,并且希望使用 LINQ 进行某种惰性评估。
请您帮我指点一下吗?
干杯, -麦克风
I need to average 3 IEnumerable collection and create a new IEnumerable with the result as follows:
var result1 = GetResult1()//Returns an enumerable collection having elements 1,2,3,4
var result2 = GetResult2()//Returns an enumerable collection having elements 3,4,2,6
var result3 = GetResult3()//returns an enumerable collection having elements 2,5,1,6
//I need to create a collection which has the averages of the above results as below:
var result4 = GetResult4()//Should return 2.00,3.67,2.33,5.33.
Each results will be having the same number of elements i.e result1, result2, result3 will be having the same number of elements (in this case 4, but actually it can range till 1500).
Each element in the result corresponds to the average value of the results i.e in result4 collection the first element will be an average of 1,3,2 which corresponds to the average of the first element in result1,result2,result3 respectively and hence forth.
The resultant collection should be having decimal places rounded to 2.
An obvious answer is to iterate through each element in the collections and create the elements in the resultant collection manually (i.e via a for loop), however I want to avoid it as the results will have close to 1500 elements in each collection and would like to do some sort of lazy evaluation using LINQ.
Please can you help me with some pointers?
Cheers,
-Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 .NET 4,则可以在此处使用
Enumerable.Zip
以获得良好效果:这不会进行任何舍入 - 这通常更适合格式化 步骤。请注意,无论如何,这些值只能以 0、0.33 或 0.67 结尾,因为您只能除以 3。这里我使用了
double
,但另一种选择是使用decimal< /code> - 这取决于值的真正含义。
If you're using .NET 4, you can use
Enumerable.Zip
to good effect here:This won't do any rounding - that would normally be more appropriate for a formatting step. Note that the values can only end with 0, 0.33, or 0.67 anyway, as you're only dividing by 3. Here I've used
double
but another option would be to usedecimal
- it depends on what the values really mean.实现此目的的一种方法是编写一个方法,该方法接受列表的列表,并执行必要的工作。类似于:
用法将是
实时示例: http://rextester.com/IGOSE98291
注意:不包括四舍五入@Jon 提到的原因相同;舍入是格式化/显示步骤。
One way to achieve this is to write a method which takes a list of lists, and does the necessary work. Something like:
Usage would then be
Live example: http://rextester.com/IGOSE98291
NB: Not included rounding for the same reason @Jon mentioned; rounding is a formatting/display step.