List of data structure lists - how do I access linq?

I was given a C # program from a software manufacturer that accesses a file and gives me a list of lists of data structures

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   

etc. inside them, I have another list of "indexes" that contain the actual data structure

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

so now I need to access part X, Y, Z of the data table 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);

to give me the average value of all the Z values ​​that are contained in the β€œindices” [0] - [10], each index of which contains hundreds or thousands of secondary indices, each of which has [x] [y] [z] values?

edit: foreach, , , linq . .

+4
4

:

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

0-10, :

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

, :

RasterSet.SelectMenu(rs => rs.Take(11)).Average(x => x.Z)
+1

I suggest the following code

RasterSet.Take(10).SelectMany(x => x.Z).Average();

You can also use the Skip () function if you want to swap

+1
source

Something like this will be ...

var avg = from i in RasterSet
      let innerRasterSet = RasterSet[i]
      where i > 0 && i <= 10
      select innerRasterSet.Average(z=> z.Z);
0
source

Source: https://habr.com/ru/post/1661051/


All Articles