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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/967099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:25:42  来源:igfitidea点击:

GroupBy with linq method syntax (not query syntax)

c#linqtranslationlinq-query-syntax

提问by Dane O'Connor

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 }

采纳答案by mqp

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

回答by Andrew Hare

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

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

回答by Lasse V. Karlsen

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. Doing that gives me the following code:

这里的技巧是转到 Reflector 的选项页面,并要求它显示 C# 2.0 语法,它将用适当的静态方法调用替换 Linq。这样做给了我以下代码:

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 @mquandershowed, but with Reflector, at least you should be able to understand the method calls being involved.

现在,当然可以使用 lambdas 和类似的代码来编写更漂亮的代码,就像@mquander展示的那样,但是使用 Reflector,至少您应该能够理解所涉及的方法调用。

回答by Dudi Keleti

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

我知道这是一个老问题,但对于新读者,请查看此 gitub代码。

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

这使用 Roslyn 获取查询语法并将其转换为扩展方法语法。