Bad Dub Bad Dub - 1 year ago 55
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 =>;

Or is it better to do this

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


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download