首页 > 其他分享 >Linq 分组GoupBy并且求和Sum

Linq 分组GoupBy并且求和Sum

时间:2022-10-28 17:56:48浏览次数:45  
标签:OrderID orderList Sum Linq SellerIDAA new SellerID GoupBy Order

 Linq 分组GoupBy并且求和Sum

List<Order> orderList = new List<Order>();
            for (int i = 0; i < 3; i++)
            {
                Order orderEntity = new Order();
                orderEntity.OrderID = 100 + i;
                orderEntity.SellerID = i;
                orderList.Add(orderEntity);
            }

            Order orderEntity2 = new Order();
            orderEntity2.OrderID = 400;
            orderEntity2.SellerID = 4;
            orderList.Add(orderEntity2);

            Order orderEntity3 = new Order();
            orderEntity3.OrderID = 400;
            orderEntity3.SellerID = 4;
            orderList.Add(orderEntity3);


 //查询分组后的结果
            var groupSellerID = from t in orderList
                                group t by new { SellerIDAA = t.SellerID } into g
                                select g;

            //拿到分组中的最大值
            var groupSellerID2 = from t in orderList
                                 group t by new { SellerIDAA = t.SellerID } into g
                                 select new Order
                                 {
                                     SellerID = g.Key.SellerIDAA,
                                     OrderID = g.Max(v => v.OrderID)
                                 };

            //各个分组的最大值相加
            var groupSellerID3 = (from t in orderList
                                  group t by new { SellerIDAA = t.SellerID } into g
                                  select new Order
                                  {
                                      SellerID = g.Key.SellerIDAA,
                                      OrderID = g.Max(v => v.OrderID)
                                  }).Sum(su => su.OrderID);

-----------------------------------------

 public class Order
    {
        public int OrderID { get; set; }
        public int SellerID { get; set; }

    }

==================================================================

//订单总运费
                //按SellerID分组,取每组中运费最大值,最后每组最大值相加。
                decimal tradePostFee = (from t in addTradeAndOrderRequstA.requestOrderAddList
                                   group t by new { SellerIDAA = t.SellerID } into g
                                   select new AddOrderRequest
                                   {
                                       SellerID = g.Key.SellerIDAA,
                                       PostFee = g.Max(v => v.PostFee)
                                   }).Sum(su => su.PostFee);

 

搜索

复制

标签:OrderID,orderList,Sum,Linq,SellerIDAA,new,SellerID,GoupBy,Order
From: https://www.cnblogs.com/jankie1122/p/11108735.html

相关文章

  • C# Linq分页
     C#Linq分页//搜索结果listDicResultList<Dictionary<string,string>>listDicResult=newList<Dictionary<string,string>>();......
  • Leetcode第907题:子数组的最小值之和(Sum of subarrays minimums )
    解题思路既然我们不能先遍历区间,然后找最小值,那么我们不如顺序倒过来,对于每个值,我们找有多少区间里面,它是最小值。对于一个数字A[i]来说,如果在某个区间[j,k]里面它......
  • C#新语法进阶委托泛型LinqLambda表达式
    学习C#开发的程序员们,相信大家也有自己的困惑。C#对于我们来说,一会很熟悉,一会很陌生,当你熟悉的时候,你阅读源码能一目十行,当你很陌生的时候,你十天能看懂一行代码。我们总是伴......
  • Kafka Consumer细节
    pollIO模型与内部线程Consumer消费多个来自多个Topic的多个分区的数据,在新版本中,它使用类似select、epoll这种IO模型来达到用一个线程管理多个来源的数据的功能。不过,Co......
  • LeetCode_Array_64. Minimum Path Sum (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxngridfilledwithnon-negativenumbers,findapathfromtopleftt......
  • LC 907 Sum of Subarray Minimums
    DecrisptionGivenanarrayofintegersarr,findthesumofmin(b),wherebrangesoverevery(contiguous)subarrayofarr.Sincetheanswermaybelarge,retu......
  • Leetcode第862题:和至少为K的最短子数组(Shortest Subarray with sum at least k)
    解题思路前缀和定义前缀和\(s[0]=0\),\(s[i+1]=\displaystyle\sum\limits_{j=0}^inums[j]\)。例如\(nums=[1,2,-1,2]\),对应的前缀和数组为\(s=[0,1,3,2,4]\)。......
  • Luogu P4868 Preprefix sum
    题目链接:​​传送门​​线段树维护前缀和简单明了修改就修改当然还有更快的树状数组差分的做法*/#include<iostream>#include<cstdio>#include<cstring>#include<cs......
  • Kafka Server之kafka-console-consumer.bat
    KafkaServer之kafka-console-consumer.bat注意:博主使用kafka版本:kafka_2.12-3.3.1.tgzwindows版一、订阅主题全部消息(注意:Producer已经生产:0~4999共5000条消息)在k......
  • E. Permutation by Sum
    传送门题意:给出n,l,r,s,要求构造一个序列,要求满足l,r区间的和是s,存在就是输出序列,否则就-1思路:首先判断是否-1,很简单,就是一个区间里面的最大值和最小值,s必须......