c# - 按两列分组并使用元组作为键 C# Linq 执行 ToDictionary

标签 c# .net linq ormlite-servicestack

我有一段代码,我想按两个字段分组,然后用这两个字段作为元组键对其执行 ToDictionary。我不确定语法。以下是我所拥有的,但问题是它创建了一个包含单个项目的元组。

var count = this.Db.Query<EmployeeCount>(@"select
            employername, ein, month, headcount
            from employerInfo A inner join MonthlyInfo B on (A.Id = B.Id)
            where A.ClientId = @Client",
            new { run.Client })
            .GroupBy(r => new { r.EIN, r.EmployerName})
            .ToDictionary(pair => Tuple.Create<string>(pair.Key.ToString()), pair => pair.ToDictionary(r => (Months)r.month, r => r.headcount));

我的 EmployeeCount 类是

 private class CountQuery
    {
        public string EmployerName;
        public string EIN;
        public int Month;
        public int HeadCount;
    }

我尝试创建一个 Tuple.Create,但我不确定如何通知元组的参数是 EIN 和 Employername。

最佳答案

我自己想出来的如下

var count = this.Db.Query<EmployeeCount>(@"select
        employername, ein, month, headcount
        from employerInfo A inner join MonthlyInfo B on (A.Id = B.Id)
        where A.ClientId = @Client",
        new { run.Client })
        .GroupBy(r => new { r.EIN, r.EmployerName}).ToDictionary(pair => Tuple.Create<string,string>(pair.Key.EIN.ToString(),pair.Key.EmployerName), pair => pair.ToDictionary(r => (Months)r.ReportingMonth, r => r.FTECount))

关于c# - 按两列分组并使用元组作为键 C# Linq 执行 ToDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345391/

相关文章:

C# AES 加密字节数组

c# - 在打开的 DataReader 中执行另一个 MySQL 语句

c# - 调用 Sitefinity WCF Web 服务时出现错误 401

c# - 在 WCF REST 服务中返回非 JSON、非 XML 数据

.net - Windows 窗体 DataGridView 的 ItemDataBound?

vb.net - 如何使用Linq ToDictionary返回字典项中具有多个值的字典?

javascript - 获取 ASP 文本框的更改值

c# - Silverlight 类库到 Windows Phone 7 类库

c# - Linq 生成给定日期范围内缺失记录的列表

linq - 从 Azure 云表删除时出错 - ResourceNotFound