【问题标题】:Group by two columns and do a ToDictionary with a Tuple as Key C# Linq按两列分组并使用元组作为键 C# Linq 执行 ToDictionary
【发布时间】:2016-02-11 16:54:57
【问题描述】:

我有一段代码,我想按两个字段分组并在其上使用两个字段作为元组键对其执行 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 和元组的雇主名称。

【问题讨论】:

  • 仅供参考,您应该使用带有公共 properties 的 POCO,即使用 OrmLite 而不是 fields

标签: c# .net linq ormlite-servicestack


【解决方案1】:

我自己想出来的如下

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))

【讨论】:

    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多