Nao*_*aor 109 .net c# lambda entity-framework
如何使用lambda将多列分组?
我看到了如何使用linq实现它的示例,但我正在寻找lambda形式.
Adu*_*cci 225
var query = source.GroupBy(x => new { x.Column1, x.Column2 });
Run Code Online (Sandbox Code Playgroud)
小智 16
我想出了像 David 的答案那样定义一个类的混合方案,但不需要一个Where 类来配合它。它看起来像:
var resultsGroupings = resultsRecords.GroupBy(r => new { r.IdObj1, r.IdObj2, r.IdObj3})
.Select(r => new ResultGrouping {
IdObj1= r.Key.IdObj1,
IdObj2= r.Key.IdObj2,
IdObj3= r.Key.IdObj3,
Results = r.ToArray(),
Count = r.Count()
});
private class ResultGrouping
{
public short IdObj1{ get; set; }
public short IdObj2{ get; set; }
public int IdObj3{ get; set; }
public ResultCsvImport[] Results { get; set; }
public int Count { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
resultRecords我要分组的初始列表在哪里,它是一个List<ResultCsvImport>. 请注意,这里的想法是,我按 3 列进行分组:IdObj1、IdObj2 和 IdObj3
如果你的表是这样的
rowId col1 col2 col3 col4
1 a e 12 2
2 b f 42 5
3 a e 32 2
4 b f 44 5
var grouped = myTable.AsEnumerable().GroupBy(r=> new {pp1 = r.Field<int>("col1"), pp2 = r.Field<int>("col2")});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139330 次 |
| 最近记录: |