LINQ groupby式を作成しようとしているので、VS2010コードサンプルのDyanmic LINQを使用しています。ユーザーが実行時にグループ化する/グループ化するプロパティとグループ化の期間(年、四半期、月)を選択できるようにしたいと思います。このため、ダイナミックLINQを使用することにしました。
これが私のデータのサンプルです:
ID |収入| OrDate |
1 | 900000 | 1-1-2000 |
2 | 180000 | 1-9-2000 |
3 | 300000 | 2002年3月2日|
4 | 100000 | 2003年2月7日|
と私が構築する式は次のようになります:
dataSource.GroupBy("new (OrDate.Year as Year, OrDate as Month)", "Income").Select("new (Key.Year, Key.Month, Sum(Value) as Income)");
これは、リストの正確なタイプを知っている場合にのみ機能します。例: List<Product>
またはList<Order>
。私の問題は、コンパイル時のdataSourceタイプがList<object>
です。このため、リストにOrdersまたはProductsを入力すると、例外が発生します。
No generic method 'GroupBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
これをどのように回避し、ソースリストをList<object>
として維持できるか誰にも教えてもらえますか?
これを試して -
For group by in LinQ
var x = t.GroupBy(gp => gp.Name).OrderBy(group => group.Key).Select(group => Tuple.Create(group.Key, group.Count()));
見つけてください