gpt4 book ai didi

c# - 返回计数列表并按 id linq 分组到 sql

转载 作者:行者123 更新时间:2023-11-30 23:23:57 25 4
gpt4 key购买 nike

我有一张表格,上面是电话 list :

public class CHECKLIST{
[Key]
public int CL_ID { get; set; }

public EmpID { get; set; }
}

我正在尝试返回如下所示的列表。

EMPID   COUNT
1 5
2 7
3 10

我正在尝试将存储库编写为字典并在 g=> g.Count of cannot convert lambda expression to type IEqualityComparer because it's not a delegate type 出现错误:

    public Dictionary<int, int> GetAllComplaintsCount()
{
try
{
return _context.Checklists
.GroupBy(a => a.MonitorEnteredEmpID)
.ToDictionary(g => g.Key, g => g.Count);
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}

最佳答案

如果你改变这个:

ToDictionary(g => g.Key, g => g.Count);

以下内容:

ToDictionary(g => g.Key, g => g.Count());

你会得到你想要的。您应该这样做的原因取决于 GroupBy 返回的结果。根据specification

The group clause returns a sequence of IGrouping objects that contain zero or more items that match the key value for the group. For example, you can group a sequence of strings according to the first letter in each string. In this case, the first letter is the key and has a type char, and is stored in the Key property of each IGrouping object. The compiler infers the type of the key.

您想要的是获取每个组中的项目数。由于您有一个序列,因此您可以通过调用 Count 方法来获取序列中项目的数量。

关于c# - 返回计数列表并按 id linq 分组到 sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38059654/

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