[英]how to group by nested properties using linq
I have the following classes:我有以下课程:
public class Split
{
public int Id { get; set; }
public virtual ICollection<Sort> Sorts { get; set; }
public double Mass { get; set; }
}
public class Sort
{
public int Id { get; set; }
public virtual ICollection<Fraction> Fractions { get; set; }
}
public class Fraction
{
public int Id { get; set; }
public Method Method { get; set; }
}
public class Method
{
public int Id { get; set; }
public string Code { get; set; }
}
I would like to get something like the following selection/grouping of Splits for all Methods:我想为所有方法获得类似于以下拆分选择/分组的内容:
{Id, Mass, MethodCode}
. {Id, Mass, MethodCode}
。 It's fine for split
to repeat for example: split
可以重复,例如:
1, 4.4, "Samm"
1, 4.4, "Mani"
2, 2.1, "Samm"
3, 1.5, "Pra"
In the end, I want to be able to take this list and ask something like: give me splits such that their method is "samm".最后,我希望能够使用这个列表并提出类似的问题:给我拆分,以便他们的方法是“samm”。 If you can suggest a better structure, it's also fine.
如果你能提出更好的结构,那也很好。 I will fetch this from SQL server, so I am looking for the most performant structure.
我将从 SQL 服务器中获取它,因此我正在寻找性能最高的结构。
I'm not sure why you need Sort
or Fraction
but here's how you can get those values from that structure where splits
is an IEnumerable<Split>
.我不确定为什么需要
Sort
或Fraction
但这里是如何从splits
是IEnumerable<Split>
结构中获取这些值的方法。
var result = from s in splits
from r in s.Sorts
from f in r.Fractions
select new
{
s.Id,
s.Mass,
f.Method.Code
};
Or in method syntax或者在方法语法中
var result = splits.SelectMany(
s => s.Sorts.SelectMany(
r => r.Fractions.Select(
f => new
{
s.Id,
s.Mass,
f.Method.Code
})));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.