简体   繁体   English

Linq group by 带返回计数

[英]Linq group by with return count

Net core and Linq.网芯和Linq。 I have table like below我有如下表

Orders Table订单表

OrderId   Status
1         New
2         New
3         In Progress
4         In Progress
5         Closed
6         Closed

I have below model我有以下 model

public class SummaryEntity
 { 
  public int New { get; set; }
  public int InProgress { get; set; }
  public int Closed { get; set; }
 }

Then I need to return and bind to below model like below然后我需要返回并绑定到 model 下面,如下所示

New : 2
InProgress : 2
Closed : 2 

I have tried something like below我已经尝试过类似下面的东西

SummaryEntity result = (from item in Orders
                          group item by new { item.Status } into g
                          select new SummaryEntity{ //not sure how to get count and assign it to model }
                           );

I am finding hard to group by and assign values to model.我发现很难对 model 进行分组和赋值。 Can someone help me to write query.有人可以帮我写查询。 Any help would be appreciated.任何帮助,将不胜感激。 Thank you谢谢

You've already grouped the items by status, so g.Key will contain the status value and g itself is an enumerable of the grouped items.您已经按状态对项目进行了分组,因此g.Key将包含状态值,而g本身是分组项目的可枚举。 If you want to calculate their count use Count() , eg:如果您想计算他们的计数,请使用Count() ,例如:

var counts = from item in Orders
             group item by new { item.Status } into g
             select new {status=g.Key, count=g.Count()};

This will return one object per status value with its count.这将返回一个 object 每个状态值及其计数。 Getting different columns for each status is essentially pivoting, converting the rows to columns.为每个状态获取不同的列本质上是旋转,将行转换为列。

In this case though, where you know the status names in advance, you can convert the results into a dictionary and retrieve the counts by name, eg:但是在这种情况下,如果您事先知道状态名称,则可以将结果转换为字典并按名称检索计数,例如:

var dict=counts.ToDictionary(x=>x.status,x=>x.count);

var model= new SummaryEntity 
           {
               New        = dict.TryGetValue("New",out var c_n)
                            ? c_n : 0,
               InProgress = dict.TryGetValue("InProgress",out var c_p)
                            ? c_p : 0,
               Closed     = dict.TryGetValue("Closed",out var c_c)
                            ? c_c : 0,
           };

Dictionary.TryGetValue is used to avoid exceptions if a status value is missing Dictionary.TryGetValue用于在缺少状态值时避免异常

You model makes no sense.你 model 没有意义。 You have only status not New, InProgress, Closed.您只有状态不是 New、InProgress、Closed。 Try following:尝试以下操作:

            DataTable dt = new DataTable();
            dt.Columns.Add("OrderId", typeof(int));
            dt.Columns.Add("Status", typeof(string));
            
            dt.Rows.Add( new object[] { 1, "New"});
            dt.Rows.Add( new object[] { 2, "New"});
            dt.Rows.Add( new object[] { 3, "In Progress"});
            dt.Rows.Add( new object[] { 4, "In Progress"});
            dt.Rows.Add( new object[] { 5, "Closed"});
            dt.Rows.Add( new object[] { 6, "Closed"});

            var results = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Status"))
                .Select(x => new { status = x.Key, count = x.Count() })
                .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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