views:

34

answers:

2

I have an entity with three columns, a, b, and c. If I project the ObjectSet to objects of an anonymous class:

var ret = e.foos.Select(x => new {
    a = x.a,
    b = x.b
}).ToList();

Then the actual SQL only includes the columns necessary to populate each object:

SELECT 
[Extent1].[a] AS [a], 
[Extent1].[b] AS [b]
FROM [dbo].[foo] AS [Extent1]

If I include the EntityObject as a property of the anonymous class, then all columns in the entity are included in the SQL, even though only "a" and "b" are called out explicitly:

var ret = e.foos.Select(x => new {
    a = x.a,
    b = x.b,
    o = x
}).ToList();

SELECT 
[Extent1].[a] AS [a], 
[Extent1].[b] AS [b], 
[Extent1].[c] AS [c]
FROM [dbo].[foo] AS [Extent1]

Is there a way I can exclude column "c" from being fetched from the database while still having a reference to the EntityObject in my anonymous object?

+1  A: 

Perhaps if you think about what should happen when you go ret.o.c, you'd realise that it doesn't make sense for Entity to behave the way you want.

Igor Zevaka
+1  A: 

Are you asking if it's possible to get an entity reference that hasn't had its properties populated? I don't think that's supported. However, you get almost as much benefit from just retrieving the primary key of your entity class, which should be all that you need to identify your entity anyway.

Jacob