c# - 单个键的多个值

标签 c# .net linq list

我有一个包含类别及其接受/拒绝计数的列表,但此列表存在问题。我正在使用 LINQ 查询来访问数据,并按类别名称和接受/拒绝代码 (ResultCode) 对它们进行分组。所以数据是这样的形式:

enter image description here

几乎所有类别都有 AP 计数和 RJ 计数。我要做的是显示每个类别的接受和拒绝计数。我应该使用什么?哈希表不适合这个问题,我尝试使用 int List 作为值的字典,但是当出现相同的键时无法添加。

更新:

List<ModReportingDM.ReportObjects.AllCategories> allcats = new List<ModReportingDM.ReportObjects.AllCategories>();
Dictionary<string, ModReportingDM.ReportObjects.ResultCode> dict = new Dictionary<string, ModReportingDM.ReportObjects.ResultCode>();
ModReportingDM.ReportObjects.ResultCode x = new ModReportingDM.ReportObjects.ResultCode();
allcats = reportBLL.GetAllCats(model.ModId, model.ReportStartDate, model.ReportEndDate);
        if (allcats != null)
        {
            model.AllCatsList = new List<ModReportingDM.ReportObjects.AllCategories>();

            foreach (var item in allcats)
            {
                x.Accepted = item.Count;
                x.Rejected = item.Count;
                dict.Add(item.Category, x);

            }
        }

查询:

public List<AllCategories> GetAllCats(int modId, DateTime startDate, DateTime endDate)
    {
        using (entities = new ModReportingEntities())
        {
            var query = (from c in entities.Content
                         where c.ModId == modId && c.CreatedTime >= startDate && c.CreatedTime <= endDate && c.Category != null
                         group c by new { c.Category, c.ResultCode } into g
                         orderby g.Count() ascending
                         select new AllCategories
                         {
                             Category = g.Key.Category,
                             ResultCode = g.Key.ResultCode,
                             AcceptCount = g.Count(),
                             RejectCount = g.Count()
                         });

            return query.ToList();
        }
    }

最佳答案

我会做的是创建一个 ResultCode类:

public class ResultCode
{
    public int Ap { get; set; }
    public int Rj { get; set; }
}

然后使用 Dictionary<string, ResultCode>它将每个类别映射到其报告。

您也可以采用不同的方法使用 Tuple<T1, T2> (我个人不太喜欢)它只是将您的 key 映射到两个不同的值:

Dictionary<string, Tuple<int, int>> categoryToResultCode;

关于c# - 单个键的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542159/

相关文章:

c# - asp.net mvc 的 ReturnUrl 问题

c# - TextBox 中不显示换行符

.net - 将上下文菜单命令参数绑定(bind)到数据网格属性

c# - 源不包含数据行

c# - 在 LINQ c# 中按 rollno 文本排序?

c# - 日期时间转换

c# - 无法解析unicode字符

c# - 如何从 C# (.net 3.5) 连接 Node.js 服务器

c# - 从代码覆盖率中排除类 - .runsettings

c# - 如何查看 IQueryable 表达式的参数