开发者

Including the EntityObject in a LINQ To Entities Projection Without Including All Columns

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, ev开发者_StackOverflow中文版en 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜