通过外部类集合内部类属性使用LINQ to循环 [英] Using LINQ to loop through inner class properties in outer class collection
问题描述
利用关闭Q&放大器;与通过一个对象的属性循环处理(的 http://stackoverflow.com/questions/15586123/loop-through-object-and-get-properties ,的循环通过一个对象属性在C#中),其中有:
- 的Class1对象的集合(即listObj1)
- 每1级包含的Class2对象的集合(即dictObj2)
你将如何:
- 有效地确定内部类的属性(等级2)通过内部类的属性(等级2)
- 循环遍历的Class1对象的集合(listObj1)选择的所有实例
- 循环等级2财产
- 等级2财产输出集合(如:
- 和组收集由Class1.PropertyA和Class1.PropertyB
>
请在下面找到所涉及的类的粗略地图。
我一直在努力使用LINQ查询没有成功。 。任何意见或指导意见,将不胜感激。
类MainClass {
名单,LT; 1级> listObj1
}
类class1 {
//多个领域,包括...
INT PropertyA {搞定;组; }
INT PropertyB {搞定;组; }
&字典LT; INT,Class2中> dictObj2 {搞定;组; }
}
类的Class2 {
//一个号码的所有类型的双...
双MeasurementA {领域获得;组; }
双MeasurementB {搞定;组; }
双MeasurementC {搞定;组; }
}
由于数据:
MainClass mainClass =新MainClass();
mainClass.listObj1 =新的List< 1级>()
{
新的Class1(){
dictObj2 =新词典< INT,Class2中>(){
{ 1,新的Class2(){MeasurementA = 2.0,MeasurementB = 3.0,MeasurementC = 4.0}},
{2,新的Class2(){MeasurementA = 5.0,MeasurementB = 6.0,MeasurementC = 7.0}}
}
}
};
您可以使用LINQ写:
VAR栏= typeof运算(等级2)
//获取ClassB的
.GetProperties的属性(BindingFlags.Public | BindingFlags.Instance)
//每个地图成的PropertyInfo从c1.dictObj2 $ b $其价值的集合体b .SelectMany(PI => mainClass.listObj1
.SelectMany(C1 => c1.dictObj2)
。选择(p =>新的
{
房产= pi.Name,
值= pi.GetValue(p.Value)
}))
//集团对于属性名数据
.GroupBy(X => x.Property,X => x.Value)
//,创造正确的字典
.ToDictionary(X => x.Key中,x => x.ToList());
和现在你有一个词典
的钥匙的 ClassB的
属性名称和值列表
这些属性值。
Leveraging off the Q&As dealing with looping through an object's properties (http://stackoverflow.com/questions/15586123/loop-through-object-and-get-properties, Loop Through An Objects Properties In C#), where you have:
- a collection of Class1 objects (i.e. listObj1)
- each Class1 contains a collection of Class2 objects (i.e. dictObj2)
How would you:
- efficiently determine the properties of the inner class (Class2)
- loop through the the properties of the inner class (Class2)
- loop through the collection of Class1 objects (listObj1) selecting all instances of the the Class2 property
- output the collection of Class2 property (e.g. the first iteration would return a collection of MeasurementA, one from each Class1 object).
- and group the collection by Class1.PropertyA and Class1.PropertyB
Please find below a rough map of the classes involved.
I have been trying to use a LINQ query without success. Any ideas or guidance would be greatly appreciated.
class MainClass {
List<Class1> listObj1
}
class Class1 {
// a number of fields including...
int PropertyA { get; set; }
int PropertyB { get; set; }
Dictionary<int, Class2> dictObj2 { get; set; }
}
class Class2 {
// a number of fields all of type double...
double MeasurementA { get; set; }
double MeasurementB { get; set; }
double MeasurementC { get; set; }
}
Given data:
MainClass mainClass = new MainClass();
mainClass.listObj1 = new List<Class1>()
{
new Class1() {
dictObj2 = new Dictionary<int,Class2>() {
{ 1, new Class2() { MeasurementA = 2.0, MeasurementB = 3.0, MeasurementC = 4.0 }},
{ 2, new Class2() { MeasurementA = 5.0, MeasurementB = 6.0, MeasurementC = 7.0 }}
}
}
};
you can write with LINQ:
var fields = typeof(Class2)
// Get Properties of the ClassB
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
// Map each PropertyInfo into collection of its values from c1.dictObj2
.SelectMany(pi => mainClass.listObj1
.SelectMany(c1 => c1.dictObj2)
.Select(p => new
{
Property = pi.Name,
Value = pi.GetValue(p.Value)
}))
// Group data with respect to the PropertyName
.GroupBy(x => x.Property, x => x.Value)
// And create proper dictionary
.ToDictionary(x => x.Key, x => x.ToList());
and now you have a Dictionary
with keys of ClassB
property names and values as List
of those properties values.
这篇关于通过外部类集合内部类属性使用LINQ to循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!