2

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...

FooUser
  • 53
  • 5
  • How is `Foo` defined? Also, with the code in the question I'm not able to repro, the type arguments cannot be deduced for the call to `test` from `Main`. – Serge Belov Nov 21 '12 at 21:09
  • This code can be used with **LinqPad** `Foo` can be replaced with a table name `Baz` can be replaced with a column in said table. I'm running code in **LinqPad** works ok, need a reference to LinqKit.dll and `System.Linq.Expressions` and `LinqKit` namespaces. – FooUser Nov 21 '12 at 21:50
  • Thanks, it makes sense, I managed to repro in LinqPad. – Serge Belov Nov 21 '12 at 22:20

1 Answers1

1

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);
FooUser
  • 53
  • 5