linq groupby()和Sum()

时间:2016-05-17 12:42:01

标签: c# linq group-by

这可能非常简单,但我无法让它工作(linq级新手)。

我想对同一天落在客户端的行进行分组和求和(日期以epoc / unixtime显示)。

目前它的最大值为21(从Kbytes转换为Gbytes),而不是±26,这是我想要看到的。

如何添加GroupBySum子句以添加每天的Kbytes?

public class Images
{
    public string ClientName { get; set; }
    public string ImageID { get; set; }
    public int PolicyType { get; set; }
    public int ScheduleType { get; set; }
    public Int64 BackupTime { get; set; }
    public Int64 KBytes { get; set; }
}

public static List<Images> images = new List<Images>();

    public void calc()
    {

        foreach (var client in clients)
        {
            totalmax = 0;
            totalavg = 0;
            foreach (var ptype in ptypes)
            {

                var windowsquery = images
                    .Where(s => s.PolicyType == Convert.ToInt16(ptype) && s.ScheduleType == 0 && s.ClientName == client)
                    .Select(s => s.KBytes);

                double typemax = windowsquery.DefaultIfEmpty().Max()/1048576;
                totalmax = Math.Round(totalmax + typemax);
                double typeavg = windowsquery.DefaultIfEmpty().Average()/1048576;
                totalavg = Math.Round(totalavg + typeavg);

            }
            Console.WriteLine("Client = {0}, Max = {1}, Ave = {2}", client,totalmax, totalavg);
            Console.ReadKey();
        }
    }

输出(从输出中省略其他客户端)

客户端= Client2,Max = 21,Ave = 14

更新:已实施此

var summary = (from image in images
                           where image.ScheduleType == 0
                           group image by new
                           {
                               image.ClientName,
                               image.PolicyType,
                               image.ScheduleType,
                               BackupDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0)
                                                                       .AddSeconds(image.BackupTime)
                                                                       .ToString("yyyy-MM-dd")
                           } into imageGroup
                           select new
                           {
                               ClientName = imageGroup.Key.ClientName,
                               PolicyType = imageGroup.Key.PolicyType,
                               BakupDateTime = imageGroup.Key.BackupDateTime,
                               SumGBytes = imageGroup.Sum(s => s.KBytes) / 1048576.0,
                               AvgGBytes = imageGroup.Average(s => s.KBytes) / 1048576.0,

                           }
                                    ).ToList();

但总和不正确,平均值可以正常工作。

Console Ouput

Excel Comparison

3 个答案:

答案 0 :(得分:0)

你已经试过这个吗?

 var windowsquery = images
                    .Where(s => s.PolicyType == Convert.ToInt16(ptype) 
                     && s.ScheduleType == 0 && s.ClientName == client)
                    .GroupBy(s => s.date)
                    .Select(s => s.Sum(y=>y.KBytes));

在您所包含的图片中,日期似乎不正确或是特殊格式?

答案 1 :(得分:0)

似乎重复发帖。答案已经在Stack溢出中可用了......

var windowsquery = images
                .Where(s => s.PolicyType == Convert.ToInt16(ptype) && s.ScheduleType == 0 && s.ClientName == client).GroupBy(s => YourDateField)
                .Select(s => s.Sum(i => i.KBytes));

 var windowsquery = images
                        .Where(s => s.PolicyType == Convert.ToInt16(ptype) && s.ScheduleType == 0 && s.ClientName == client).GroupBy(s => YourDateField)
                        .Select(g=>new { sum = g.Sum(k=>k.KBytes), field = g.Key });

答案 2 :(得分:0)

检查一下:

var summary = (from image in images
               group image by new { image.ClientName, 
                                    image.PolicyType,
                                    BackupDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0)
                                                .AddSeconds(image.BackupTime)
                                                .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
                                  } into imageGroup
               select new { ClientName = imageGroup.Key.ClientName, 
                            PolicyType = imageGroup.Key.PolicyType,
                            BakupDateTime = imageGroup.Key.BackupDateTime,
                            SumGBytes = imageGroup.Sum(s => s.KBytes) / 1048576.0,
                            AvgGBytes = imageGroup.Average(s => s.KBytes) / 1048576.0,
                            MaxGBytes = imageGroup.Max(s => s.KBytes) / 1048576.0,
                            Records = imageGroup.Count()
                          }
              ).ToList();

您可以使用以下查询列出已转换BackupTime的所有记录:

var listWithDateTime = (from image in images
                        orderby image.ClientName ascending, 
                                image.PolicyType ascending, 
                                image.BackupTime ascending
                        select new { 
                                 ClientName = image.ClientName,
                                 PolicyType = image.PolicyType,
                                 GBytes = image.KBytes / 1048576.0,
                                 UnixTimestamp = image.BackupTime,
                                 NormalDate = new DateTime(1970, 1, 1, 0, 0, 0, 0)
                                            .AddSeconds(image.BackupTime)
                                            .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture) 
                               }
                       ).ToList();