LINQ SelectMeny包含索引属性的对象列表 [英] LINQ SelectMeny on list of object containing indexed property

查看:77
本文介绍了LINQ SelectMeny包含索引属性的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我发现我的问题很难解释:)

但是我会试着给我。



我有一个列表,其中包含几个PeakData类型的项目,我已经完成了以下代码。

我想对所有项目使用linq selectmeny索引,对象内索引属性的偏移。



但是我看不到必须这样做。



Hi

I find my question really hard to explain :)
But i will give i at try.

I have a list containing several items of type PeakData, i have attected the code below.
I would like to do a linq selectmeny on all item using Index, Offset on the indexed property inside the object.

But i can't see have to do it.

public class Handling
{
    private List<PeakData> Data = new List<PeakData>();

    internal Handling(List<PeakData> Data)
    {
        this.Data = Data;

        //Code
    }
}





我试过这样做但它不起作用



I have try doing this but it dosen't work

var Items= Items.SelectMany(x => x[Index,Offset]).ToArray();










public class PeakData
{
   public const int HyperionChannels = 16;   

   private double[][] Data = new double[HyperionChannels][];

   public double this[int Index, int Offset]
   {
     get
     {
        return Data[Index][Offset];
     }
     set
     {
        Data[Index][Offset] = value;
     }
   }
}

推荐答案

假设您已经解决了从未初始化的明显问题你的锯齿状数组结构中的内部数组,'数据:你可以在'PeakData类中使用这样的静态方法返回一个数组,其中包含'List< PeakData>的数据字段中的所有双值:
Assuming you have fixed the obvious problem of your never initializing the inner arrays in your jagged array structure, 'Data: you can use a static method like this in the 'PeakData class to return an array containing all the double values in the 'Data field of a List<PeakData>:
// requires Linq
using System.Linq;

public static double[] GetAllValues(List<PeakData> peakDatas)
{
   return peakDatas.SelectMany(ary => ary.Data.SelectMany(val => val)).ToArray();
}

这里有两个扁平化阶段:第一个'SelectMany将返回所有内部数组;第二个'SelectMany将把所有这些变成一个数组。



据我所知,你不能使用你实现的自动索引器来单独初始化内部数组(对于当你使用'PeakData实例'时,十六个外部数组中的每一个都是。但是,我可能错了;事实上,我无法让它工作并不意味着这里真正的大师之一无法让它发挥作用!



使用通用列表是我的首选这里而不是Arrays。

There's two stages of "flattening" here: the first 'SelectMany is going to return all the inner arrays; the second 'SelectMany is going to flatten all those into one array.

To my knowledge you cannot use the automatic indexer you have implemented to initialize the inner arrays individually (for each of the sixteen outer arrays) as you work with an instance of 'PeakData. However, I could be wrong on that; the fact I could not get that to work does not mean that one of the real gurus here cannot get it to work !

It would be my preference to use generic Lists here rather than Arrays.


第一眼看,您的代码应该产生编译器错误 CS0236 字段初始化程序无法引用非静态字段,方法或属性字段 [ ^ ]。你没有提到......



我建议回到基础并阅读:使用构造函数(C#编程指南) [ ^ ]
On the first look, your code should produce compiler error CS0236: A field initializer cannot reference the nonstatic field, method, or property 'field'[^]. You did not mention that...

I'd suggest to get back to basics and read about: Using Constructors (C# Programming Guide)[^]


这篇关于LINQ SelectMeny包含索引属性的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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