GroupBy with linq method syntax (not query syntax)
Asked Answered
V

4

20

How would the following query look if I was using the extension method syntax?

var query = from c in checks
group c by string.Format("{0} - {1}", c.CustomerId, c.CustomerName) 
into customerGroups
select new { Customer = customerGroups.Key, Payments = customerGroups }
Vacla answered 8/6, 2009 at 21:21 Comment(2)
For your future reference, this question is answered in the C# 3.0 specification, which you can download off of the internet. All the query transformation rules are specified there.Garfish
ty, didn't know the transformation rules were documented.Dreg
D
26

It would look like this:

var query = checks
    .GroupBy(c => string.Format("{0} - {1}", c.CustomerId, c.CustomerName))
    .Select (g => new { Customer = g.Key, Payments = g });
Doublecheck answered 8/6, 2009 at 21:24 Comment(0)
K
8

First, the basic answer:

var query = checks.GroupBy<Customer, string>(delegate (Customer c) {
    return string.Format("{0} - {1}", c.CustomerId, c.CustomerName);
}).Select(delegate (IGrouping<string, Customer> customerGroups) {
    return new { Customer = customerGroups.Key, Payments = customerGroups };
});

Then, how do you figure out these things yourself?

First, download Reflector from here, and install it.

Then build a sample program, like a smallish console program, containing the code you want to analyze. Here's the code I wrote:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication11
{
    public class Customer
    {
        public Int32 CustomerId;
        public Int32 CustomerName;
    }

    class Program
    {
        static void Main(string[] args)
        {
            var checks = new List<Customer>();
            var query = from c in checks
                        group c by String.Format("{0} - {1}", c.CustomerId, c.CustomerName)
                            into customerGroups
                            select new { Customer = customerGroups.Key, Payments = customerGroups };
        }
    }
}

Then you build that, and open reflector, and ask it to open the .exe file in question.

Then you navigate to the method in question, which in my case was ConsoleApplication11.Program.Main.

The trick here is to go to the options page of Reflector, and ask it to show C# 2.0 syntax, which will substitute Linq with the appropriate static method calls. Doing that gives me the following code:

private static void Main(string[] args)
{
    List<Customer> checks = new List<Customer>();
    var query = checks.GroupBy<Customer, string>(delegate (Customer c) {
        return string.Format("{0} - {1}", c.CustomerId, c.CustomerName);
    }).Select(delegate (IGrouping<string, Customer> customerGroups) {
        return new { Customer = customerGroups.Key, Payments = customerGroups };
    });
}

Now, of course this code can be written a bit prettier with lambdas and similar, like what @mquander showed, but with Reflector, at least you should be able to understand the method calls being involved.

Kieserite answered 8/6, 2009 at 21:31 Comment(1)
ReSharper also has conversion to/from query syntax.Afrit
T
2

Since the compiler does this translation for you, fire up Reflector and take a look.

Tass answered 8/6, 2009 at 21:24 Comment(0)
C
0

I know this is a old question, but for new readers, take a look at this gitub code.

This use Roslyn to take query syntax and convert it to extension method syntax.

Charr answered 8/11, 2016 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.