简体   繁体   English

GroupBy 与 linq 方法语法(不是查询语法)

[英]GroupBy with linq method syntax (not query syntax)

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 }

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

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.首先,从这里下载 Reflector 并安装它。

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.然后你构建它,打开反射器,并要求它打开有问题的 .exe 文件。

Then you navigate to the method in question, which in my case was ConsoleApplication11.Program.Main .然后导航到有问题的方法,在我的例子中是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.这里的技巧是转到 Reflector 的选项页面,并要求它显示 C# 2.0 语法,它将用适当的静态方法调用替换 Linq。 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.现在,当然可以使用 lambdas 和类似的代码来编写更漂亮的代码,就像@mquander 展示的那样,但是使用 Reflector,至少您应该能够理解所涉及的方法调用。

由于编译器会为您完成此翻译,因此请启动Reflector并查看一下。

I know this is a old question, but for new readers, take a look at this gitub code.我知道这是一个老问题,但对于新读者,请查看此 gitub代码。

This use Roslyn to take query syntax and convert it to extension method syntax.这使用 Roslyn 获取查询语法并将其转换为扩展方法语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM