Sorry for the bad title...This one is probably best explained with an example:
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");
}
}
}
How come this works when toLinkTo
is declared locally, but using the exact same IQueryable
as a property on another object doesn't? The exception I get is:
Unable to create a constant value of type 'ClassA'. Only primitive types ('such as Int32, String, and Guid') are supported.
Thanks in advance.