Francisco Hanna
Francisco Hanna

Reputation: 1137

Are chained IEnumerable's methods Where and Select slower than doing them separately?

I have some inherited code with the following calls:

First:

var pasajesDelServicio = venta.Pasajes.Where(p =>
                        p.ServicioPasaje.ClaveSolicitud == servicio.RequestKey &&
                        p.ServicioPasaje.IndiceServicio == servicio.ServiceIndex);

Then:

DatosPasajeros = pasajesDelServicio.ToList().Select(p => {
                            p.Pasajero.TipoDePasajero = (Dominio.Entidades.Cliente.TipoDePasajero) solicitudVennta.IdTipoPasajero;
                            return new DatosPasajeroCompra
                            {
                                Butaca = p.NumeroAsiento,
                                Pasajero = p.Pasajero,
                                IdVentaDetalle = p.IdVentaDetalle,
                                SubeEn = p.SubeEn,
                                MenuABordo = p.MenuABordo?.Id,
                                Precio = p.PrecioBruto
                            };
                        })

Would it be more efficient it the Where and Select calls were chained? Like so:

DatosPasajeros = venta.Pasajes.Where(p =>
                            p.ServicioPasaje.ClaveSolicitud == servicio.RequestKey &&
                            p.ServicioPasaje.IndiceServicio == servicio.ServiceIndex)
                            .ToList()
                            .Select(p => {
                            p.Pasajero.TipoDePasajero = (Dominio.Entidades.Cliente.TipoDePasajero) solicitudVennta.IdTipoPasajero;
                            return new DatosPasajeroCompra
                            {
                                Butaca = p.NumeroAsiento,
                                Pasajero = p.Pasajero,
                                IdVentaDetalle = p.IdVentaDetalle,
                                SubeEn = p.SubeEn,
                                MenuABordo = p.MenuABordo?.Id,
                                Precio = p.PrecioBruto
                            };
                        })

I've tried reading trough some docs, but couldn't find my answer. I'm really new to C#.

Upvotes: 2

Views: 71

Answers (2)

Kieran Devlin
Kieran Devlin

Reputation: 1433

Function chaining compiles the same as calling the functions individually. If you take a look at the interface for the IEnuerableExtensions, you'll see that its just an extension method that takes this IEnumerable source. C# Extension methods are just syntactic sugar and will compile as if the function's were called individually. As others have mentioned, its best to not call .ToList<T>() in your case as its a waste.

Upvotes: 1

Canica
Canica

Reputation: 2738

The chaining itself wouldn't save you much processing since the exact same code will be executed in either case. However, since you have a pasajesDelServicio and a DatosPasajeros variables you would save some memory by only have one variable. Additionally, where you may be able to make this more efficient is by moving the ToList() call after your Select returning only a subset of the data instead of the entire entity (and doing the filtering after).

HTH

Upvotes: 1

Related Questions