gpt4 book ai didi

c# - 动态 LINQ GroupBy 问题

转载 作者:行者123 更新时间:2023-11-30 18:42:40 25 4
gpt4 key购买 nike

我正在使用 VS2010 代码示例中的 Dyanmic LINQ,因为我正在尝试创建 LINQ groupby 表达式。我想让用户能够在运行时选择要分组的属性以及分组的时间段(年、季度、月)。因此,我决定使用 Dynamic LINQ。

这是我的数据示例:

编号|收入 |日期 |
1 | 900000 | 1-1-2000 |
2 | 180000 | 1-9-2000 |
3 | 300000 | 2002 年 3 月 2 日 |
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> .我的问题是编译时的数据源类型是 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()));

请找

https://dotnetfiddle.net/WPqzhJ

关于c# - 动态 LINQ GroupBy 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com