0
私は、次のLINQクエリを書いた:IGroupingの結果を書くには?
public IEnumerable DailyReturns()
{
var a = from exec in executions
group exec by exec.TimeStamp.Date into execGroup
select new { TimeStamp = execGroup.Key, CashFlow = execGroup.Sum(e => e.CashFlow())};
return a;
}
私は未処理の例外を取得:System.InvalidCastException: Specified cast is not valid.
私がしようとする:死刑執行がList<Execution>
ある
foreach (KeyValuePair<DateTime, double> p in s.Executions.DailyReturns())
{
Console.WriteLine(p.Key.ToString() + ',' + p.Value.ToString());
}
。あなたはKeyValuePair<DateTime,double>
ではなく、匿名型を返すされている
public DateTime TimeStamp { get; set; }
public double CashFlow()
{
return Price * Quantity * -1;
}
'選択https://docs.microsoft.com/en-us/({}' [匿名型]を生成する新しいですdotnet/csharp/programming-guide/classes-and-structs/anonymous-types) - それは 'KeyValuePair'ではありませんのでキャストできません! –