C#Linq-将所有已分组的数据分组 [英] C# Linq - Group allready grouped data

查看:686
本文介绍了C#Linq-将所有已分组的数据分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数据进行了分组,如下所示:

I've groupted data which now looks like this:

<tr>
    <th>Id</th>
    <th>Type</th>
    <th>Amount</th>
</tr>
<tr>
    <td>1</td>
    <td>Delivered</td>
    <td>100</td>
</tr>
<tr>
    <td>2</td>
    <td>Sent</td>
    <td>150</td>
</tr>
<tr>
    <td>3</td>
    <td>Received</td>
    <td>110</td>
</tr>
<tr>
    <td>4</td>
    <td>Delivered</td>
    <td>79</td>
</tr>
<tr>
    <td>5</td>
    <td>Sent</td>
    <td>30</td>
</tr>

这是我如何获取的代码:

Here's the code how I get it:

var query = await _context.product.AsNoTracking().Where(x => (x.productDate.Date >= DateTime.Now.AddDays(-30) && x.productDate.Date <= DateTime.Now.Date).ToListAsync();

if (query == null) return null;

var data = query.GroupBy(x => x.productStatusId)
           .Select(product => new productsChartDTO
           {    
               Type =
                (
                    product.FirstOrDefault().productStatusId == (int)Enums.productStatus.Delivered || product.FirstOrDefault().productStatusId == (int)Enums.productStatus.InProcess ? "Delivered" :
                    product.FirstOrDefault().productStatusId == (int)Enums.productStatus.Sent || product.FirstOrDefault().productStatusId == (int)Enums.productStatus.InProcess ? "Sent" :
                    product.FirstOrDefault().productStatusId == (int)Enums.productStatus.Received ? "Received" : "Unknown"
                ),
               Amount = product.Sum(x => x.Amount)
           });  
return data;

即使我按productStatusId分组,也有可能见到伙计,如果productStatus在'InProcess'或'Delivered'中,则将出现类型"Delivered",因此在此之后询问 我在示例中看到的两次看到的是类型为"Delivered"的结果.

As it's possible to seen guys, even if I grouping by productStatusId, if productStatus is in 'InProcess' or in 'Delivered' there will be Type "Delivered", so after this query I'm seeing results with type "Delivered" twice as I posted in example.

我认为执行此操作后,我应该再次将数据分组吗?

I think after this execution I should be grouping data again?

我怎么能做到这一点?

How could I achieve this ?

是否可以继续对此查询进行分组,或者应该有另一个可以再次分组的查询?

Is it possible to continue grouping on this query or there should be another one which would group again?

谢谢

欢呼

推荐答案

只需将逻辑移至GroupBy方法并从中返回类型string而不是int:

Just move your logic to the GroupBy method and return a type string instead of an int from it:

var data = query
    .GroupBy(x => x.productStatusId == (int)Enums.productStatus.Delivered || x.productStatusId == (int)Enums.productStatus.InProcess ? "Delivered" :
                x.productStatusId == (int)Enums.productStatus.Sent || x.productStatusId == (int)Enums.productStatus.InProcess ? "Sent" :
                x.productStatusId == (int)Enums.productStatus.Received ? "Received" : "Unknown")
    .Select(product => new productsChartDTO
    {
        Type = product.Key,
        Amount = product.Sum(x => x.Amount)
    });

这篇关于C#Linq-将所有已分组的数据分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆