使用Linq使用动态键对数据进行分组 [英] Grouping data with dynamic keys with Linq
本文介绍了使用Linq使用动态键对数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用System.Linq.Dynamic使用两个代码将对象列表分组为对象列表的新分组列表:
I'm using System.Linq.Dynamic to group a list of objects into a new grouped list of list of objects with the both code:
Dim lstGroupedFilterNew = lstDataSource _
.GroupBy("New (Unit_ID, Itinerary_ID Driver_ID)", "grp") _
.[Select](Function(grp) grp.ToList()).ToList()
但是我遇到错误 lambda表达式无法转换为'string',因为'string'不是我的select子句上的委托类型.
推荐答案
我选择不使用Linq.Dynamic,我发现了一种验证条件以动态地将密钥添加到GroupBy子句的方法,结果是这样的:>
I chose not to use Linq.Dynamic, I discovered a way to verify conditions to dynamically add the keys to my GroupBy clause, the result is this:
Dim lstGroupedFilter = (From t In lstDataSource Group t By __groupByKey1__ = _
New With {Key .Unit_ID = (If(blnUnit, t.Unit_ID, 0)), Key .Itinerary_ID = If(blnItinerary, t.Itinerary_ID, 0), Key .Driver_ID = If(blnDriver, t.Driver_ID, 0)} _
Into g = Group Select g.ToList()).ToList()
This is an option to dynamically add keys and generate a result similar to this solution.
这篇关于使用Linq使用动态键对数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文