数据结构列表列表-如何使用linq访问? [英] list of lists of data structure - how do i access with linq?

查看:64
本文介绍了数据结构列表列表-如何使用linq访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从软件制造商处获得了一个c#程序,该程序可以访问文件并为我提供数据结构列表的列表

i have been given a c# program from a software manufacturer that accesses a file and gives me a list of lists of datastructures

List<List<dataPt_struct>> RasterSet = new List<List<dataPt_struct>>();

这给了我类似的索引"列表

this gives me a list of "indexes" something like

RasterSet Count = 100
[0]Count = 400
[1]Count = 411   

,依此类推.在这些里面,我还有另一个索引"列表,其中包含实际的数据结构

and so on. inside those i have another list of "indexes" which contain the actual data structure

[0]
   [X] 
   [Y] 
   [Z] 
...
[399]
   [X]
   [Y]
   [Z]

所以现在我需要访问列表list内的数据结构的X,Y,Z部分.例如,是否可以使用LINQ说

so now i need to access the X,Y,Z portion of the datastruct inside the list of lists . For example, is it possible to use LINQ to say

if (RasterSet[i] >= 0 && Rasterset[i] =< 10)
RasterSet[i].Average(z=> z.Z);

给出索引" [0]至[10]中包含的所有Z值的平均值,其中每个索引具有成百上千个次级索引,每个次级索引具有[x] [ y] [z]个值?

to give me a an average of all the Z values that are contained in the "indexes" [0] to [10], each index of which has hundreds or thousands of secondary indexes, each of which has [x][y][z] values?

edit:我通过根据复杂结构制作普通列表,然后在该列表上使用普通linq拼凑了一个双foreach.我仍然很想一口气拿到整个东西.谢谢大家的建议

edit: i've cobbled together a double foreach that works by making a normal list from the complex structure and then using normal linq on that list. i'd still love to get the whole thing in one go. thanks everyone for the suggestions

推荐答案

获取总平均值:

var average = RasterSet.SelectMany(x => x).Average(x => x.Z);

要获取索引0-10的子列表的平均值,请执行以下操作:

To get the average of the sub lists from Index 0-10, do this:

var average = RasterSet.GetRange(0, 10).SelectMany(x => x).Average(x => x.Z);

这篇关于数据结构列表列表-如何使用linq访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆