LINQ按字符串分组并计算分组属性的子属性 [英] LINQ Group by string and count grouped property's child property

查看:77
本文介绍了LINQ按字符串分组并计算分组属性的子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了如下代码:

 Session["priceRange"] = ranges.Select(r => new PriceRangeGraph
                {
                    Price = Math.Round(r, 2),
                    Sales = lista.Where(x => ranges.FirstOrDefault(y => y >= x.SalePrice) == r).Sum(x => x.SaleNumber),
                    SuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() > 0).Count(),
                    UnSuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() == 0).Count(),
                }).ToList();

这是两个有问题的代码行:

SuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() > 0).Count(),
UnSuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() == 0).Count(),

如您所见,在销售物业中,我正在找到商品的销售价格范围,然后我将给定范围内的所有销售额相加.

现在,我正在尝试查看有多少成功/失败用户(及其用户名)在给定范围内进行了销售.

So for example user test123 made 5 sales, test1234 made 4 sales, test56 made 0 sales in range 0-20$

此范围的输出为:

SuccessfulSellers=2
UnSuccessfulSellers = 1

我上面尝试的代码根本无法给我正确的结果...

如您所见,我正在按用户名进行分组以获取发生次数,然后过滤用户进行销售的范围,然后简单地添加另一个和语句,以过滤销售额为= 0的用户,那些销售额超过0的人...

我在做什么错了?

解决方案

我认为您的问题是操作顺序之一.分组之后,实际上您在处理一个更为复杂的数据结构:基本上是一个列表列表.保存您的群组,直到使用完过滤器为止,您的生活将会变得更加轻松.

例如:

allSales.Where(m => m.Price >= 0 && m.Price <= 20)
     .GroupBy(m => m.User)
     .Select(m => new { User = m.Key, Sales = m.Count() });

I have written a code like below:

 Session["priceRange"] = ranges.Select(r => new PriceRangeGraph
                {
                    Price = Math.Round(r, 2),
                    Sales = lista.Where(x => ranges.FirstOrDefault(y => y >= x.SalePrice) == r).Sum(x => x.SaleNumber),
                    SuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() > 0).Count(),
                    UnSuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() == 0).Count(),
                }).ToList();

These are the two problematic lines of code:

SuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() > 0).Count(),
UnSuccessfulSellers = lista.GroupBy(x => x.StoreName).Where(x => ranges.FirstOrDefault(y => y >= x.Select(z => z.SalePrice).FirstOrDefault()) == r && x.Select(h => h.SaleNumber).FirstOrDefault() == 0).Count(),

In sales property as you can see I'm finding the price range where the item was sold and then I simply sum all sales within the given range.

Now I'm trying to see how many successful/unsuccessful users (with their usernames) have made sales within that given ranges.

So for example user test123 made 5 sales, test1234 made 4 sales, test56 made 0 sales in range 0-20$

The output for this range would be:

SuccessfulSellers=2
UnSuccessfulSellers = 1

The code above that I tried doesn't gives me correct results at all...

As you can see I'm grouping by the user's username to get the occurance number, and then filter the range for which user had made the sale, and then simply add another and statement to filter out those with =0 sales and those with more than 0 sales...

What am I doing wrong here?

解决方案

I think your issue is one of order of ops. Once you group by, you're actually dealing with a much more complex data structure: a list of lists, basically. Save your group by until after your filter(s), and your life will be much easier.

For example:

allSales.Where(m => m.Price >= 0 && m.Price <= 20)
     .GroupBy(m => m.User)
     .Select(m => new { User = m.Key, Sales = m.Count() });

这篇关于LINQ按字符串分组并计算分组属性的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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