Selecting part of one object without extracting the entire object

I want to get one property (blob) from one object (by identifier). I have:

context.References
    .Single(r => r.ID == id)
    .Blob;

This seems ineffective to me because I get the whole link only to drop everything except Blob. This led to

context.References
    .Where(r => r.ID == id)
    .Select(r => r.Blob)
    .Single();

Which should be requested only by Blob, but the presence of Single as an afterthought at the end is somewhat annoying (but it is necessary to observe the singularity, which I consider necessary). My question is this: is there a better way to accomplish this, or is my second code block exactly as it is?

Thank!

+3
source share
2 answers

I am afraid that it is so. Executing your queries in LINQPad shows that the Entity Framework translates queries to this:

SELECT TOP (2) 
[Extent1].[Id] AS [Id], 
[Extent1].[Blob] AS [Blob], 
... etc for all columns
FROM [dbo].[References] AS [Extent1]
WHERE 1 = [Extent1].[Id]

and

SELECT TOP (2) 
[Extent1].[Blob] AS [Blob]
FROM [dbo].[References] AS [Extent1]
WHERE 1 = [Extent1].[Id]

, , . , , .

+1

context.References.Single(r => r.ID == id).Blob Where Single, . .

+1

All Articles