我正在尝试使用实体框架生成此查询dbContext
:
SELECT id
FROM menuitems
WHERE (parent_id = 11)
UNION
SELECT id
FROM menuitems AS menuitems_2
WHERE (parent_id IN
(SELECT id
FROM menuitems AS menuitems_1
WHERE (parent_id = 11)))
表 menuitems 有 2 列:id、parent_id
我试过这个:
List<int> mi = ctx.MenuItems.Where(i => i.parent_id == this.id).Select(id => id.id)
.Union(ctx.MenuItems.Where(c => ctx.MenuItems
.Where(i => i.parent_id == this.id).Select(id => id.id)
.Contains((int)c.parent_id)).Select(id => id.id))
.ToList();
但它不起作用,因为我收到以下错误:
Unable to create a constant value of type 'Menuitem'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
有没有办法生成这个查询?