✅ Ef-core query efficiency?
I have a question regarding querying data. Which one is more efficient since I only need the department ids. The first one
or second one
15 Replies
The first one, since you only select specific fields.
Note that in the first statement
AsNoTracking
is redundant, an entity is not tracked when Select
is usedGood to hear.
Thanks for the input mate.
First one hands down
The second one will just load everything, including the related data
So, why
I see, when I write queries like this, it also means the database selects the properties I want right?
Yep
You can check what exact query EF will generate
yes, EF will log the translated SQL queries to the console
if the log level is
Information
@Angius Sorry for pinging you, how come the log looks like this
From which query?
the first one.
Ah
Probably because of your use of this method
Try doing the transformation into a DTO directly in that
Select
Ok let me try it.
Alternativaly, use an
Expression<Func<TSource, TTarget>>
And pass that to .Select
Or use something like this: https://github.com/ClaveConsulting/Expressionify Yeah that solved it. Thank you very much. I had no Idea that MapToDto was the issue.
:Ok: