[英]Dynamic LINQ GroupBy question
我正在使用VS2010代碼示例中的Dyanmic LINQ,因為我正在嘗試創建一個LINQ groupby表達式。 我希望允許用戶在運行時選擇要分組的屬性以及分組的時間段(年,季度,月)。 因此,我決定選擇Dynamic LINQ。
這是我的數據樣本:
ID | 收入| OrDate |
1 | 900000 | 1-1-2000 |
2 | 180000 | 1-9-2000 |
3 | 300000 | 3-2-2002 |
4 | 100000 | 2-7-2003 |
我構建的表達式如下:
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>
?
嘗試這個 -
For group by in LinQ
var x = t.GroupBy(gp => gp.Name).OrderBy(group => group.Key).Select(group => Tuple.Create(group.Key, group.Count()));
請找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.