【问题标题】:Linq list of interface, interface has member an array of strings, I want linq statement that aggregates all strings from array of objectsLinq 接口列表,接口有成员字符串数组,我想要 linq 语句聚合对象数组中的所有字符串
【发布时间】:2020-09-16 03:06:35
【问题描述】:

我在 C# 中有一个接口数组,在接口中有一个成员,一个字符串列表,我试图将具有字符串数组属性的接口成员数组中的所有字符串添加在一起。

IPackingFlowEvaluation[]


public interface IPackingFlowEvaluation
{
    IPackingFlow PackingFlow { get; }

    int Priority { get; }

    bool CanUse { get; }

    string[] Reasons { get; }
}

我想要一个 linq 语句,它将接口成员数组的所有字符串添加到一个大的字符串列表中。一个巨大的原因列表,其中包含来自接口数组的所有原因,该接口的成员本身具有字符串数组。

【问题讨论】:

    标签: c# arrays linq object


    【解决方案1】:

    听起来你在关注SelectMany

    var reasons = source.SelectMany(e => e.Reasons).ToList();
    

    其中source 指的是IPackingFlowEvaluation[]

    【讨论】:

      【解决方案2】:

      您需要使用SelectMany() 来实现该效果。

      var allReasons = array.SelectMany(i => i.Reasons.ToList()).ToList();
      

      【讨论】:

      • SelectMany 中的 ToList() 是多余的。没有它你会过得更好。
      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多