C# 如何在c中使用Linq或lambda表达式获取对象的第二/第三个内部列表的总计数#
我有一个这样的班级结构C# 如何在c中使用Linq或lambda表达式获取对象的第二/第三个内部列表的总计数#,c#,linq,lambda,C#,Linq,Lambda,我有一个这样的班级结构 class ABC { public List<A> LA { get; set; } } class A { public List<B> LB { get; set; } } class B { public List<C> LC { get; set; } } class C { public int x { get; set; } public int y { get; set; }
class ABC
{
public List<A> LA { get; set; }
}
class A
{
public List<B> LB { get; set; }
}
class B
{
public List<C> LC { get; set; }
}
class C
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}
ABC类
{
公共列表{get;set;}
}
甲级
{
公共列表LB{get;set;}
}
B类
{
公共列表LC{get;set;}
}
C类
{
公共整数x{get;set;}
公共整数y{get;set;}
公共int z{get;set;}
}
在这里,您可以看到类“abc”可以有类“A”的多个对象/元素,类“A”的每个元素可以有类“B”的多个元素,类“B”的每个元素可以有类“C”的多个元素。现在,我想计算类“C”的元素总数,并使用Linq或lambda表达式将该总数存储在int类型变量中,而不使用foreach循环。您可以在每个列表中求和
abc.LA.Sum(xabc=>xabc.LB.Sum(xb=>xb.LC.Count))
下面是一个你如何做到这一点的例子
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
class ABC
{
public List<A> LA { get; set; }
}
class A
{
public List<B> LB { get; set; }
}
class B
{
public List<C> LC { get; set; }
}
class C
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}
public static void Main()
{
var abc = new ABC
{
LA = new List<A>
{
new A
{
LB = new List<B>
{
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
},
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
}
}
},
new A
{
LB = new List<B>
{
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
},
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
}
}
}
}
};
Console.WriteLine(abc.LA.Sum(xabc => xabc.LB.Sum(xb => xb.LC.Count)));
}
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
公共课程
{
ABC班
{
公共列表{get;set;}
}
甲级
{
公共列表LB{get;set;}
}
B类
{
公共列表LC{get;set;}
}
C类
{
公共整数x{get;set;}
公共整数y{get;set;}
公共int z{get;set;}
}
公共静态void Main()
{
var abc=新abc
{
LA=新列表
{
新的
{
LB=新列表
{
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
},
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
}
}
},
新的
{
LB=新列表
{
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
},
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
}
}
}
}
};
WriteLine(abc.LA.Sum(xabc=>xabc.LB.Sum(xb=>xb.LC.Count));
}
}
您可以在每个列表中求和
abc.LA.Sum(xabc=>xabc.LB.Sum(xb=>xb.LC.Count))
下面是一个你如何做到这一点的例子
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
class ABC
{
public List<A> LA { get; set; }
}
class A
{
public List<B> LB { get; set; }
}
class B
{
public List<C> LC { get; set; }
}
class C
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}
public static void Main()
{
var abc = new ABC
{
LA = new List<A>
{
new A
{
LB = new List<B>
{
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
},
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
}
}
},
new A
{
LB = new List<B>
{
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
},
new B
{
LC = new List<C>
{
new C { x =1, y= 2, z= 3},
new C { x =1, y= 2, z= 3}
}
}
}
}
}
};
Console.WriteLine(abc.LA.Sum(xabc => xabc.LB.Sum(xb => xb.LC.Count)));
}
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
公共课程
{
ABC班
{
公共列表{get;set;}
}
甲级
{
公共列表LB{get;set;}
}
B类
{
公共列表LC{get;set;}
}
C类
{
公共整数x{get;set;}
公共整数y{get;set;}
公共int z{get;set;}
}
公共静态void Main()
{
var abc=新abc
{
LA=新列表
{
新的
{
LB=新列表
{
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
},
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
}
}
},
新的
{
LB=新列表
{
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
},
新B
{
LC=新列表
{
新的C{x=1,y=2,z=3},
新的C{x=1,y=2,z=3}
}
}
}
}
}
};
WriteLine(abc.LA.Sum(xabc=>xabc.LB.Sum(xb=>xb.LC.Count));
}
}
abc.LA.SelectMany(LA=>LA.LB).SelectMany(LB=>LB.LC.Count() abc.LA.SelectMany(LA=>LA.LB).SelectMany(LB=>LB.LC.Count() 到目前为止你试过什么?您是否有示例数据?@PavelAnikhouski您发布了一个答案,然后将其删除。。。那太好了。到目前为止你试过什么?您是否有示例数据?@PavelAnikhouski您发布了一个答案,然后将其删除。。。那太完美了。。