简体   繁体   English

Linq分组由多个列组成,并在group by子句之前从表中获取额外值

[英]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提供ExtraColumnExtraColumn2值。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM