私は次のクラス構造を持っています:
public class PriceLog
{
public DateTime LogDateTime {get; set;}
public int Price {get; set;}
}
List< PriceLog >の場合、Linq クエリで、以下に示すデータと同等の出力を生成する必要があります。
LogDateTime | AVG(価格)
2012年1月 | 2000
2012 年 2 月 | 3000
簡単に言うと、その年の各月の平均価格を計算したいのです。
注: LogDateTime プロパティは次のようにフォーマットする必要があります。LogDateTime.ToString("MMM yyyy")
次のことを試しましたが、目的の結果が得られるかどうかはわかりません。
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)};