Does breaking chained Select()s in LINQ to objects hurt performance?

Posted by Justin on Stack Overflow See other posts from Stack Overflow or by Justin
Published on 2010-05-04T05:13:17Z Indexed on 2010/05/04 5:18 UTC
Read the original article Hit count: 206

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 Query.Records(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.

© Stack Overflow or respective owner

Related posts about linq-to-objects

Related posts about .NET