破坏 LINQ to 对象中的链接 Select() 是否会损害性能?

发布于 2024-08-31 16:56:03 字数 836 浏览 8 评论 0原文

采用以下伪 C# 代码:

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

public IEnumerable<IDataRecord> GetRecords(string sql)
{
     // DB logic goes here
}

public IEnumerable<IEmployer> Employers()
{
     string sql = "select EmployerID from employer";
     var ids = GetRecords(sql).Select(record => (record["EmployerID"] as int?) ?? 0);
     return ids.Select(employerID => new Employer(employerID) as IEmployer);
}

如果组合两个 Select() 调用,会更快吗?上面的代码中是否有额外的迭代?下面的代码是不是更快一些?

public IEnumerable<IEmployer> Employers()
{
     string sql = "select EmployerID from employer";
     return GetRecords(sql).Select(record => new Employer((record["EmployerID"] as int?) ?? 0) as IEmployer);
}

我认为如果性能没有差异,第一个示例更具可读性。

Take the following pseudo C# code:

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

public IEnumerable<IDataRecord> GetRecords(string sql)
{
     // DB logic goes here
}

public IEnumerable<IEmployer> Employers()
{
     string sql = "select EmployerID from employer";
     var ids = GetRecords(sql).Select(record => (record["EmployerID"] as int?) ?? 0);
     return ids.Select(employerID => new Employer(employerID) as IEmployer);
}

Would it be faster if the two Select() calls were combined? Is there an extra iteration in the code above? Is the following code faster?

public IEnumerable<IEmployer> Employers()
{
     string sql = "select EmployerID from employer";
     return GetRecords(sql).Select(record => new Employer((record["EmployerID"] as int?) ?? 0) as IEmployer);
}

I think the first example is more readable if there is no difference in performance.

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

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

发布评论

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

评论(2

堇色安年 2024-09-07 16:56:04

没有什么区别。

LINQ 使用延迟求值的思想,来源。我将引用相关部分:

为了解决这个问题,所有内置 LINQ 提供程序都使用称为延迟执行的概念。它们都只是返回实现 IEnumerable(of T) 接口的类型,而不是立即执行查询运算符。然后,这些类型会延迟执行,直到在 foreach 循环中实际使用查询。

基本上,直到您在 foreach.ToList() 等中使用 Employers() 的结果,它实际上并没有完成任何工作。

There is no difference.

LINQ uses the idea of delayed evaluation, source. I'll quote the relevant part:

To get around this, all built-in LINQ providers utilize a concept known as delayed execution. Rather than having query operators execute immediately, they all simply return types that implement the IEnumerable(of T) interface. These types then delay execution until the query is actually used in a for each loop.

Basically, until you use the result of Employers() in a foreach or .ToList() etc, it hasn't actually done any work.

绝對不後悔。 2024-09-07 16:56:03

没有显着差异。这两种方法都返回一个可以循环 GetRecords 结果的表达式。

它们并不相同,因为第一个具有链接的选择,但它们仍然会以相同的顺序执行相同的工作。循环链接的 Select 时,第二个 Select 将根据需要将第一个 Select 中的项目一个又一个放入,第一个 Select 不必在第二个 Select 可以使用结果之前完成。

There is no significant difference. Both methods returns an expression that can loop through the result from GetRecords.

They are not identical, as the first one has chained Selects, but they will still do the same work and in the same order. When looping the chained Selects, the second select will put items one and one from the first Select as needed, the first Select doesn't have to complete before the second Select can use the result.

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