请建议用 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/