【发布时间】:2022-11-09 04:02:37
【问题描述】:
我正在开发 EF Core 3.1 中的项目,但该版本可能与这个问题无关,因为我对 group by 有一个简单的例子的问题。
我的原始查询很长,所以在创建查询的过程中,我编写了稍后在select 和group-by 中使用的模型,例如:
public class GroupClass
{
public int PropA { get; set; }
}
我认为问题在于加入结果,但即使是这个模型的简单示例也不起作用:
int[] testArray = { 1, 2, 3, 3, 3, 3, 3, 3 };
var result = (from a in testArray
group a by new GroupClass
{
PropA = a,
} into g
select new ModelClass
{
PropA = g.Key.PropA,
}).ToList();
Console.WriteLine(result.Count);
Result: 8
Expected/wanted result: 3
很明显,group-by 比不上3==3,但是我该怎么办呢?我对流利的语法有同样的问题。
这是 rextester 上的完整代码:example。
【问题讨论】:
标签: c# entity-framework linq entity-framework-core grouping