简体   繁体   English

选择实体 ef linq 的属性

[英]select properties of entity ef linq

i have query for a list of objects (ex Rooms) that contains an object (ex Door) Door has many properties (ex width, height, color), but i only need color.我有一个对象列表(例如房间)的查询,其中包含一个对象(例如门)门有许多属性(例如宽度、高度、颜色),但我只需要颜色。

now i have我现在有

Rooms.include(r => r.Door) 

but this takes all properties of door.但这需要门的所有属性。 if i do如果我做

Rooms.include(r => r.Door.color) 

than it says that color is not a navigational property of door.比它说颜色不是门的导航属性。

How can i only select color ?我怎样才能只选择颜色?

i hope i made myself clear.我希望我说清楚了。 any help is appreciated任何帮助表示赞赏

Include is not used for selecting, it is used to tell the compiler that the property should be included in the query. Include 不用于选择,它用于告诉编译器该属性应包含在查询中。 Just use a select to get the color and include Rooms in the select.只需使用选择来获取颜色并在选择中包含房间。

Edit: Answer was edited after additional information was provided.编辑:在提供附加信息后编辑了答案。

Rooms.Select(r => new { Color = r.Door.color, Room = r });

Unfortunately you cannot conditionally load properties of related entity - you either load whole door entity, or don't include that entity.不幸的是,您不能有条件地加载相关实体的属性 - 您要么加载整个门实体,要么不包含该实体。 But you can use anonymous type to return room and color of it's door:但是你可以使用匿名类型来返回房间和门的颜色:

 var rooms = from r in db.Rooms
             select new {
                 Room = r,
                 DoorColor = r.Door.Color
             };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM