抱歉标题不好......这可能最好用一个例子来解释:
void Main()
{
IQueryable<ClassA> toLinkTo = context.ClassAs.Where(a => a.Name == "SomeName");
var queryToExecute = context.ClassBs.Where(cb => toLinkTo.Select(ca => ca.Id).Contains(cb.Id));
// This works.
leafNodesWithExternalChildren.ToList();
// This doesn't work.
toLinkTo = new OtherClass(context).LinkedClassAs;
leafNodesWithExternalChildren.ToList();
}
public class OtherClass
{
private MyContext m_Context;
public OtherClass(MyContext ctx)
{
this.m_Context = ctx;
}
public IQueryable<ClassA> LinkedClassAs
{
get
{
// Same as toLinkTo as it was originally declared above.
return this.m_Context.ClassAs.Where(a => a.Name == "SomeName");
}
}
}
当在本地声明时如何工作toLinkTo
,但使用与另一个对象上的属性完全相同IQueryable
的属性却没有?我得到的例外是:
Unable to create a constant value of type 'ClassA'. Only primitive types ('such as Int32, String, and Guid') are supported.
提前致谢。