gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:31:35 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com