LINQ查询与GROUP和SUM [英] LINQ Query with GROUP and SUM

查看:249
本文介绍了LINQ查询与GROUP和SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我把我的头周围使用LINQ与A组和SUM查询。

  //查询数据库
IEnumerable的<&畅销商品GT; best_sellers从BS =(db.MYDATABASE)。取(25)
                                       其中,bs.COMPANY ==我的公司
                                       通过bs.PROD code到G团BS
                                       排序依据g.Sum(g.MQTY)
                                       选择畅销书()
                                       {
                                           product_ code =,
                                           PRODUCT_DESCRIPTION =,
                                           total_quantity =
                                      };

我想:


  • 从db.MYDATABASE取前25个项目

  • 集团所有结果通过bs.PROD code

  • 的总和,每个bs.PROD code定购

  • 凡公司是我公司

  • 然后通过管道将在数据我畅销商品()对象

我很困惑,因为只要添加我中的组合,我的 BS 变量变得无用。


解决方案

  

我很困惑,因为只要添加我组的组合,我BS变量变得无用。


是的,因为你不再有一个单一的项目 - 你现在处理项目的集团的序列。您可以在第一项获得每个小组,我想会是在描述得到的有效方法是什么?

  VAR的查询=从BS在db.MYDATABASE.Take(25)
             其中,bs.COMPANY ==我的公司
             通过bs.PROD code到G团BS
             排序依据g.Sum(X => x.MQTY)
             选择新的畅销商品
             {
                 product_ code = g.Key,
                 PRODUCT_DESCRIPTION = g.First()描述,
                 total_quantity = g.Sum(X => x.MQTY)
             };

请注意,如果没有指定的排序,从db.MYDATABASE排名前25位的项目是没有意义的。 顶部在哪些方面?你可能想:

 从酒店在db.MYDATABASE.OrderByDescending(X => x.Price)。取(25)

或类似的东西。请注意,如果没有那些拥有公司的我的公司,你会没有结果落得...

或者,如果你想在排名前25位畅销书,你想在最后的拿的部分:

  VAR的查询=从db.MYDATABASE BS
             其中,bs.COMPANY ==我的公司
             通过bs.PROD code到G团BS
             排序依据g.Sum(X => x.MQTY)降
             选择新的畅销商品
             {
                 product_ code = g.Key,
                 PRODUCT_DESCRIPTION = g.First()描述,
                 total_quantity = g.Sum(X => x.MQTY)
             };
VAR TOP25 = query.Take(25);

Please help me to get my head around querying using LINQ with a GROUP and SUM.

// Query the database
IEnumerable<BestSeller> best_sellers = from bs in (db.MYDATABASE).Take(25)
                                       where bs.COMPANY == "MY COMPANY"
                                       group bs by bs.PRODCODE into g
                                       orderby g.Sum(g.MQTY)
                                       select new BestSeller()
                                       {
                                           product_code = ,
                                           product_description = ,
                                           total_quantity =  
                                      };

I wish to:

  • Take the top 25 items from db.MYDATABASE
  • Group all the results by bs.PRODCODE
  • Order it by the sum total for each bs.PRODCODE
  • Where the company is "MY COMPANY"
  • Then pipe the data in to my BestSeller() objects

I'm confused, because as soon as I add my group in to the mix, my bs variable becomes useless.

解决方案

I'm confused, because as soon as I add my group in to the mix, my bs variable becomes useless.

Yes, because you no longer have a single item - you're now processing a sequence of groups of items. You can get at first item for each group, which I assume would be a valid way of getting at the description?

var query =  from bs in db.MYDATABASE.Take(25)
             where bs.COMPANY == "MY COMPANY"
             group bs by bs.PRODCODE into g
             orderby g.Sum(x => x.MQTY)
             select new BestSeller
             {
                 product_code = g.Key,
                 product_description = g.First().DESCRIPTION,
                 total_quantity = g.Sum(x => x.MQTY) 
             };

Note that without specifying an ordering, "the top 25 items from db.MYDATABASE" makes no sense. "Top" in what way? You may well want:

from bs in db.MYDATABASE.OrderByDescending(x => x.Price).Take(25)

or something similar. Note that if none of those have a company of "MY COMPANY" you'll end up with no results...

Or if you want the top 25 bestsellers, you want the "take" part at the very end:

var query =  from bs in db.MYDATABASE
             where bs.COMPANY == "MY COMPANY"
             group bs by bs.PRODCODE into g
             orderby g.Sum(x => x.MQTY) descending
             select new BestSeller
             {
                 product_code = g.Key,
                 product_description = g.First().DESCRIPTION,
                 total_quantity = g.Sum(x => x.MQTY) 
             };
var top25 = query.Take(25);

这篇关于LINQ查询与GROUP和SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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