Bad Dub Bad Dub - 25 days ago 5
C# Question

Whats more efficient, returning all columns or casting to model in linq C#

I have a database table with 20 columns but I only need to work with 8 of the columns.

Is it more efficient to return the whole table

_context.products.where(x => x.active);


Or is it better to do this

_context.products.where(x => x.active).select(x => new SubModels.ProductItem { id = x.id, name = x.name, category = x.category etc etc});


Thanks

Answer

You can pretty much refer to this question "Why is SELECT * considered harmful".

It seems rather relevant to your case and has a lot of points.

So it's always better from performance point of view of one query to select only what you need. But you should always consider some other things like likelihood of issuing additional queries to get the data you could have returned already.

Comments