3

LINQ groupby 式を作成しようとしているときに、VS2010 コード サンプルの動的 LINQ を使用しています。ユーザーが実行時にグループ化するプロパティとグループ化の期間 (年、四半期、月) を選択できるようにしたいと考えています。このため、Dynamic LINQ を使用することにしました。

これが私のデータのサンプルです:

ID| 収入 | OrDate |
1 | 900000 | 2000 年 1 月 1 日 |
2 | 180000 | 2000 年 1 月 9 日 |
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>. このため、注文または製品でリストを埋めるたびに、例外が発生します。

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>.

4

1 に答える 1

0

これを試して -

For group by in LinQ

var x = t.GroupBy(gp => gp.Name).OrderBy(group => group.Key).Select(group => Tuple.Create(group.Key, group.Count()));

見つけてください

https://dotnetfiddle.net/WPqzhJ

于 2015-09-01T12:21:26.260 に答える