使用 linq 方法语法(不是查询语法)的 GroupBy

发布于 2024-07-23 08:55:30 字数 246 浏览 6 评论 0原文

如果我使用扩展方法语法,以下查询会是什么样子?

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 }

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 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尽揽少女心 2024-07-30 08:55:30

它看起来像这样:

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

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 });
蓝海 2024-07-30 08:55:30

首先,基本答案:

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

那么,你自己如何弄清楚这些事情呢?

首先,从此处下载 Reflector 并安装。

然后构建一个示例程序,例如一个小型控制台程序,其中包含您要分析的代码。 这是我编写的代码:

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

然后构建它,打开 Reflector,并要求它打开有问题的 .exe 文件。

然后导航到有问题的方法,在我的例子中是 ConsoleApplication11.Program.Main

这里的技巧是转到 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 };
    });
}

现在,当然可以使用 lambda 和类似的代码编写更漂亮的代码,就像 @mquander< /a> 显示,但使用 Reflector ,至少您应该能够理解所涉及的方法调用。

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.

淡忘如思 2024-07-30 08:55:30

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

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

三生一梦 2024-07-30 08:55:30

我知道这是一个老问题,但对于新读者来说,请看一下 这个 gitub< /a> 代码。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文