Question

I'm using LinqKit (http://www.albahari.com/nutshell/linqkit.aspx)

Is there a way to have the following code work without having to define a concrete class?

Trying to build a strongly typed dynamic query with LINQ to Entities.

I'm getting The parameter 'o' is not in scope. error.

in certain situations.

void Main()
{   
    var lquery = from a in Foo select new { Bar = a.Baz }; // <-- error like this
    //var lquery = from a in Foo select new stuff { Bar = a.Baz }; // <-- here no error
    test("Case", lquery, o => o.Bar).Dump();
}

class stuff { public string Bar {get; set;} }

IQueryable<T> test<T>(string val, IQueryable<T> qry, Expression<Func<T, string>> selector){
    var ex = selector.Expand();
    var b = from c in qry.AsExpandable()
            where ex.Invoke(c).Contains(val)
            select c;
    return b;
}

It appears that when an anonymous class is used with test() this error is thrown when a concrete class stuff is used then no error. Is there a workaround which would allow an anonymous class to be used in this situation?

I realize that this error might be LinkKit related but I don't have enough technical knowledge to dive in there...

Was it helpful?

Solution

In LinqKit I added a check for anonymous class to ExpressionExpander.VisitMemberAccess() to get anonymous classes to work.

relpaced

if (m.Member.DeclaringType.Name.StartsWith ("<>"))
    return TransformExpr (m);

with

string typeName = m.Member.DeclaringType.Name;
bool isAnonymous = typeName.StartsWith("<>f__AnonymousType"),
     isOuter = !isAnonymous && typeName.StartsWith("<>");
if (isOuter)
    return TransformExpr (m);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top