EF连接有效但SelectMany没有

时间:2011-12-13 05:29:51

标签: c# linq entity-framework linq-to-entities entity-framework-4.1

为什么这样做:

// find all second level categories
from c in Categories
where c.ParentId == null
join c2 in Categories on c.Id equals c2.ParentId
select c2

但是以下引发 System.NotSupportedException:无法创建类型为“Category”的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')

from c in Categories
where c.ParentId == null
from c2 in Categories
where c.Id == c2.ParentId
select c2

注意:我真正想做的是使用包含like的连接条件将表连接到自身:

from c in Categories
from c2 in Categories
where c.Lineage like c2.Lineage + '%'
select c

1 个答案:

答案 0 :(得分:0)

怎么样

from c in Categories
from c2 in Categories
where c.Lineage.StartsWith(c2.Lineage)
select c