c# - 如何将列表的属性收集到 C# 中的另一个列表?

标签 c# linq list

请建议用 C# 中另一个类的列表的属性填充列表中的类属性的最佳方法。

我有

class Source 
{
   public object A {get; set;}
   public object B {get; set;}
   public object C {get; set;}
}

class Destination
{
   public object A {get; set;}
   public object B {get; set;} // (A,B) is a unique key
   public List<object> Cs {get; set;}
   public object D {get; set;} 
}

那么我有
List <Destination> destinations; // Cs = null
List <Source> sources; //may have zero, one or more than one Cs for (A,B) 

如何用来源 C 填充目的地(或其他类)的 C?可以在这里使用 LINQ 吗?

提前致谢!

最佳答案

LINQ 来救援:

sources = destinations.SelectMany(d => d.Cs);

你可能想要
sources = destinations.SelectMany(d => 
    d.Cs.Select(c => new Source { A = d.A, B = d.B, C = c })
);

关于c# - 如何将列表的属性收集到 C# 中的另一个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14461997/

相关文章:

C#、泛型列表和继承

c# - 在 Dictionary<T1,someEnum> 和 Dictionary<T3,someEnum> 之间转换,其中 T1 :T3 Throws an Error

c# - 如何在 Windows 窗体应用程序中使用 C#.NET 正确实现 MVC

c# - 如何通过LINQ从数据集中的多个表中获取所有列名称

python - 如何在列表列表中搜索项目?

c# - 如何在 O(1) 或 O(log n) 时间内获取集合中具有最小键的元素?

c# - 使用 LINQ 在另一个数组中的数组条目的索引

c# - 可变参数的LINQ方法

python - Python 的切片边界和 "stride"之间的神秘交互

python-3.x - 从 Excel 数据创建字典