C# Linq 选择分组依据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14418142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:44:25  来源:igfitidea点击:

Linq Select Group By

c#linqgroup-by

提问by Lucifer

I have the following class structure :

我有以下类结构:

public class PriceLog
{
   public DateTime LogDateTime {get; set;}
   public int Price {get; set;}
}

For a List< PriceLog >I want a Linq query to generate an output which is equivalent to the data represented as below:

对于List< PriceLog >我想要一个 Linq 查询来生成一个输出,它相当于如下表示的数据:

LogDateTime | AVG(Price)
Jan 2012 | 2000
Feb 2012 | 3000

日志日期时间 |
2012 年 1 月的AVG(价格) | 2000
2012年2 月 | 3000

Simply : I want to compute the average price over each month of the year.
Note: LogDateTime property should be formatted as LogDateTime.ToString("MMM yyyy")

简单地说:我想计算一年中每个月的平均价格。
注意:LogDateTime 属性的格式应为LogDateTime.ToString("MMM yyyy")

I have tried the following, but not sure whether it will generate the desired result:

我尝试了以下方法,但不确定它是否会产生所需的结果:

var result = from priceLog in PriceLogList
                         group priceLog by priceLog.LogDateTime.ToString("MMM yyyy") into dateGroup
                         select new PriceLog { GoldPrice = (int)dateGroup.Average(p => p.GoldPrice), SilverPrice = (int)dateGroup.Average(p => p.SilverPrice)};

采纳答案by Sergey Berezovskiy

This will give you sequence of anonymous objects, containing date string and two properties with average price:

这将为您提供匿名对象序列,包含日期字符串和两个具有平均价格的属性:

var query = from p in PriceLogList
            group p by p.LogDateTime.ToString("MMM yyyy") into g
            select new { 
               LogDate = g.Key,
               AvgGoldPrice = (int)g.Average(x => x.GoldPrice), 
               AvgSilverPrice = (int)g.Average(x => x.SilverPrice)
            };

If you need to get list of PriceLog objects:

如果您需要获取 PriceLog 对象列表:

var query = from p in PriceLogList
            group p by p.LogDateTime.ToString("MMM yyyy") into g
            select new PriceLog { 
               LogDateTime = DateTime.Parse(g.Key),
               GoldPrice = (int)g.Average(x => x.GoldPrice), 
               SilverPrice = (int)g.Average(x => x.SilverPrice)
            };

回答by MuhammadHani

    from p in PriceLog
    group p by p.LogDateTime.ToString("MMM") into g
    select new 
    { 
        LogDate = g.Key.ToString("MMM yyyy"),
        GoldPrice = (int)dateGroup.Average(p => p.GoldPrice), 
        SilverPrice = (int)dateGroup.Average(p => p.SilverPrice) 
    }

回答by amartine

You should try it like this:

你应该像这样尝试:

var result =
        from priceLog in PriceLogList
        group priceLog by priceLog.LogDateTime.ToString("MMM yyyy") into dateGroup
        select new {
            LogDateTime = dateGroup.Key,
            AvgPrice = dateGroup.Average(priceLog => priceLog.Price)
        };

回答by yo chauhan

var result = priceLog.GroupBy(s => s.LogDateTime.ToString("MMM yyyy")).Select(grp => new PriceLog() { LogDateTime = Convert.ToDateTime(grp.Key), Price = (int)grp.Average(p => p.Price) }).ToList();

I have converted it to int because my Price field was int and Average method return double .I hope this will help

我已将它转换为 int 因为我的 Price 字段是 int 并且 Average 方法返回 double 。我希望这会有所帮助