I want to get a single property(blob) from a single entity (by Id). I have:
context.References
.Single(r => r.ID == id)
.Blob;
This strikes me as inefficient, because I’m getting the entire Reference, only to discard everything except the Blob. This led to
context.References
.Where(r => r.ID == id)
.Select(r => r.Blob)
.Single();
Which should only query for the Blob, but having the Single as an afterthought at the end is somewhat annoying (yet enforcing the singularity I feel is necessary). My question is this: is there a better way to accomplish this, or is my second codeblock just the way it is?
Thanks!
I’m afraid that’s the way it is. Running your queries in LINQPad shows that Entity Framework translates the queries to this:
and
So you’re correct that the second query is slightly more efficient. Whether this is significant is something for you to test and decide.