Linq到实体子查询

时间:2015-02-20 03:32:08

标签: entity-framework linq-to-entities entity-framework-6

如何在Linq中编写以下子查询:

Context.Set<Process>()
       .Include(...)
       .Where(x => x.Activity.Name.CompareTo(Context.Set<Activity>().Where(a => a.Id == activityId).Select(c => a.Name)) > 0)
       .Take(1);

这是一个简化版本的查询,WHERE子句只包含不起作用的部分。如果我将Context.Set()....子查询更改为字符串常量,则查询可以正常工作。实际上,它提供了NotSupportedException

LINQ to entities does not recognize method Set<Activity>

1 个答案:

答案 0 :(得分:1)

试试这个:

(from p in context.Set<Process>().Include(...)
 from a in context.Set<Activity>()
 where a.Id == activityId
 where p.Activity.Name.CompareTo(a.Name) > 0
 select p).Take(1);