gpt4 book ai didi

c# - 构建具有多个参数的 GroupBy Lambda 表达式

转载 作者:行者123 更新时间:2023-11-30 18:14:48 24 4
gpt4 key购买 nike

我有以下枚举扩展方法:

public static Expression<Func<Table, object>> GetGroupByClause(this OntDubbelingsEigenschappen o)
{
switch (o)
{
case OntDubbelingsEigenschappen.Voornaam:
return w => w.TableField1;
case OntDubbelingsEigenschappen.Achternaam:
return w => w.TableField2;
case OntDubbelingsEigenschappen.Emailadres:
return w => w.TableField3;
case OntDubbelingsEigenschappen.GeboorteDatum:
return w => w.TableField4;
default:
return null;
}
}

现在我想使用这个扩展来创建以下查询:

List<Expression<Func<Table, object>>> delList = new List<Expression<Func<Table, object>>>();
ontdubbelEigenschapIds.ForEach(f => delList.Add(f.GetGroupByClause()));
var users = Db.Table.GroupBy(x => new { delList })
.SelectMany(s => s).ToList().Select(s =>
new Model(s, isOverTakeActive)).ToList();

这不起作用,因为我在查询中传递了一个非原始类型。我怎样才能以正确的方式做到这一点?

最佳答案

Mitsu Furuta 对此有一个很好的解决方案...

https://blogs.msdn.microsoft.com/mitsu/2007/12/21/playing-with-linq-grouping-groupbymany/

虽然它不会产生扁平化的分组......

如果你想压平生成的树,你想看的组是树的叶子......尖叫着用 yield return 进行一点递归

关于c# - 构建具有多个参数的 GroupBy Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690046/

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