How to use Linq to group, sum or retrieve total. My current code returns nothing

大城市里の小女人 提交于 2020-07-22 12:49:10

问题


I have this code which I'm trying to use in retrieving the fish pond with the highest use of feed in a month, so I need to group by Pool_Name, sum feed usage and then select the highest record after sorting.

Here's an example code:

var hfeed = db.feed_fish
    .Where(x => (DbFunctions.TruncateTime(x.Feed_Time) >= frm 
              && DbFunctions.TruncateTime(x.Feed_Time) <= todat))
    .GroupBy(x => x.Pond_Name)
    .Select(y => new feed_fish
    {
        Pond_Name = y.Key,
        Total_feed_weight = y.Sum(d => d.Total_feed_weight)
     })
    .OrderByDescending(y=>y.Total_feed_weight).First();

A sample of the data...

Pond_Name      Total_Feed_Weight
Pond 1          56
Pond 2          33
Pond 1          45
Pond 2          54

What I need to return is a list or iQueryable that totals the Total_feed_weight and returns the highest so...

Pond 1      101
Pond 2      87

I should be also able to access first so I get the highest consuming pond.

Updated

 List<feed_fish> hfeed = db.feed_fish.Where(x => (DbFunctions.TruncateTime(x.Feed_Time) >= frm && DbFunctions.TruncateTime(x.Feed_Time) <= todat))
                            .ToList().GroupBy(r => r.Pond_Name, (key, enumerable) =>
                       {
                           return new feed_fish { Pond_Name = key, Total_feed_weight = enumerable.Sum(k => k.Total_feed_weight) };
                       }).OrderByDescending(t => t.Total_feed_weight).ToList();

Output

Pond Name: FarmAx_web.feed_fish. Total Feed: 

Update: To test the Where clause I just returned it as datasource to a grid and it brought the right results...

 var hfeed = db.feed_fish.Where(x => (DbFunctions.TruncateTime(x.Feed_Time) >= frm && DbFunctions.TruncateTime(x.Feed_Time) <= todat)).ToList();
                        fishwtaveragegrid.DataSource = hfeed;
                        fishwtaveragegrid.DataBind();

回答1:


I think you did it right. Since I don't have a database I just mocked your data creating a list of objects:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp8
{
    class Program
    {
        public class feed_fish
        {
            public string Pond_Name { get; set; }
            public int Total_feed_weight { get; set; }
            public override string ToString()
            {
                return $"Name: {Pond_Name } Weight: {Total_feed_weight}";
            }
        }
        static void Main(string[] args)
        {
            List<feed_fish> list = new List<feed_fish>()
            {
                new feed_fish{Pond_Name ="Pond 1", Total_feed_weight=56},
                new feed_fish{Pond_Name ="Pond 2", Total_feed_weight=33},
                new feed_fish{Pond_Name ="Pond 1", Total_feed_weight=45},
                new feed_fish{Pond_Name ="Pond 2", Total_feed_weight=54},
                new feed_fish{Pond_Name ="Pond 3", Total_feed_weight=100},
                new feed_fish{Pond_Name ="Pond 3", Total_feed_weight=200}
            };

            List<feed_fish> gruppedList = list.GroupBy(r => r.Pond_Name, (key, enumerable) =>
             {
                 return new feed_fish { Pond_Name = key, Total_feed_weight = enumerable.Sum(k => k.Total_feed_weight) };
             }).OrderByDescending(t => t.Total_feed_weight).ToList();

            foreach (var item in gruppedList)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();
        }
    }
}

And the output is (gruppedList):

Name: Pond 3 Weight: 300

Name: Pond 1 Weight: 101

Name: Pond 2 Weight: 87

I hope it helps 😊

UPDATE

Could you please try to do this and tell us if it works for you:

var hfeed = db.feed_fish.Where(x => (DbFunctions.TruncateTime(x.Feed_Time) >= frm && DbFunctions.TruncateTime(x.Feed_Time) <= todat)).ToList();

List<feed_fish> gruppedList = hfeed.GroupBy(r => r.Pond_Name, (key, enumerable) =>
             {
                 return new feed_fish { Pond_Name = key, Total_feed_weight = enumerable.Sum(k => k.Total_feed_weight) };
             }).OrderByDescending(t => t.Total_feed_weight).ToList();

            foreach (var item in gruppedList)
            {
                Console.WriteLine(item.ToString());
            }


来源:https://stackoverflow.com/questions/55509319/how-to-use-linq-to-group-sum-or-retrieve-total-my-current-code-returns-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!