user5534263
user5534263

Reputation: 148

Group by two columns and do a ToDictionary with a Tuple as Key C# Linq

I have a piece of code where I want to group by two fields and do a ToDictionary on it with the two field as a tuple key. I am not sure of the syntax. Following is what I have, But the problem is it creates a Tuple with single item.

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

And my EmployeeCount class is

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

I try to do a Tuple.Create, but i am not sure how to notify that the params would be EIN and Employername for the Tuple.

Upvotes: 1

Views: 2518

Answers (1)

user5534263
user5534263

Reputation: 148

I figured it out myself as below

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

Upvotes: 4

Related Questions