选择实体ef linq的属性 [英] select properties of entity ef linq
问题描述
我有查询包含对象(ex Door)的对象列表(ex Rooms)
门具有许多属性(宽度,高度,颜色),但我只需要颜色。
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.
现在我有
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.
如何才能选择颜色?
我希望我自己清楚。任何帮助都被赞赏
i hope i made myself clear. any help is appreciated
推荐答案
Include不用于选择,它用于告诉编译器该属性应该包含在查询。只需使用选择即可获取颜色,并在选择中包括房间。
Include is not used for selecting, it is used to tell the compiler that the property should be included in the query. Just use a select to get the color and include Rooms in the select.
编辑:在附加信息提供后,编辑了答案。
Answer was edited after additional information was provided.
Rooms.Select(r => new {Color = r.Door.color, Room = r) ;
这篇关于选择实体ef linq的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!