c# - 动态 GroupBy LINQ
问题描述
假设我有一堂课
public class Item
{
public int Field1{get;set;}
……
public int FieldN{get;set;}
}
我想像这样使用 group by;
Collection.GroupBy(selector“Field1,…,FieldK”)
.Select(x=> new Item
{
Field1 = x.Key.Field1,
…
FieldK= x.Key.FieldK,
FiledK+1= x.Sum(x.FieldK+1),
….
FiledN= x.Sum(x.FieldN)
};
我如何使用表达式树来做到这一点
解决方案
欢迎来到stackoverflow mahul!你想要的可以这样实现:
var a = from item in collection
group item by new {item.Field1, ... , item.Fieldk} into newGroup
select new {
Field1 = newGroup.Key.Field1
Field2 = newGroup.Key.Field1
...
FieldK+1 = newGroup.Sum(x => x.FieldK+1),
...
FieldN = newGroiup.Sum(x => x.FieldN)
}
确保也阅读文档。那里有很好的例子。
推荐阅读
- javascript - 使用 Emcscripten 编译 libsndfile
- javascript - setTimeout 在 useEffect 内部无限运行
- shell - 终端选项卡和终端会话(tmux)之间有区别吗?
- selenium-webdriver - 如何在 Robot Framework 中获取 Firefox devtools Network 选项卡条目?
- ios - Xcode - 该属性在此上下文中可能不可用
- python - 未加载 Linux Mint 20 驱动程序中的 PyQt5、QPSQL
- angular - Angular Unit Test:如果构造函数将 Globals.ts 文件作为参数,如何创建组件实例
- flutter - 通过 NFC 获取信用卡详细信息。飞镖
- r - 复选框 Shiny R 的反应性 k 均值分析
- gensim - gensim - ConcatenatedDoc2Vec: ModuleNotFoundError: No module named 'testfixtures