[英]Linq group by multiple columns and getting extra values from table before group by clause
I'm wondering if it's even possible here is some code sample: 我想知道这是否有可能是一些代码示例:
I've a table named Sample
: 我有一个名为
Sample
的表:
| Id| Name| Address| ExtraColumn| ExtraColumn2|
Here is class
: 这是
class
:
public class Sample{
public int Id{get;set;}
public string Name{get;set;}
public string Address{get;set;}
public float ExtraColumn{get;set;}
public float ExtraColumn2{get;set;}
}
What I have now: 我现在拥有的:
Model.Sample.GroupBy(x => new {x.Name, x.Address}).Select(x => new {x.Key, Count = x.Count()});
But I need besides Key, values of ExtraColumn
and ExtraColumn2
for each Sample
entity
in single database request
. 但是除了Key之外,我还需要为单个
database request
每个Sample
entity
提供ExtraColumn
和ExtraColumn2
值。
So the question is: Is it even possible? 所以问题是: 它甚至可能吗?
If so how to get it done? 如果是这样,如何完成它?
If I have understood you correctly, this is what you want:- 如果我理解正确,这就是你想要的: -
Model.Sample.GroupBy(x => new {x.Name, x.Address})
.Select(x =>
new
{
Name = x.Key.Name,
Address = x.Key.Address,
Count = x.Count(),
obj = x.ToList()
});
Here, obj
will contain the List<Sample>
in each group. 这里,
obj
将包含每个组中的List<Sample>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.