0

我有以下结构的A类

 public class A
  {
   public list<B> Items // where B is a class entity
  }

  public class B{
    public List<B> OwnItems;
    public List<C> Items // where C is a class entity
   }

  public class C
  {
   public string name;
   public string Address;
   public int Age;
   public double Salary;
  }

如何使用 c# 从实体 A 获取 C 类实体列表

4

2 回答 2

0

您可以在SelectMany的帮助下使用 LINQ :

A a = new A();
// Populate a's `Items` property...
....

var allC = a.Items.SelectMany(b => b.Items.Select( c => c)).ToList();
于 2013-04-06T13:11:50.507 回答
0
var a = new A();
var bList = a.Items;
var cList = new List<C>();
while(bList != null && bList.Count > 0)
{
    foreach(var b in bList)
        cList.AddRange(b.Items);

    bList = bList.SelectMany(b => b.OwnItems)
                 .Where(b => !bList.Contains(b))
                 .Distinct().ToList();
} 

对我来说不是最好的,但会起作用

于 2013-04-06T23:18:43.040 回答