在尝试创建 LINQ groupby 表达式时,我正在使用 VS2010 代码示例中的 Dyanmic LINQ。我想允许用户在运行时选择要分组的属性以及分组时间(年、季度、月)。因此,我决定使用 Dynamic LINQ。
这是我的数据示例:
身份证| 收入 | 日期 |
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>
?