Question

I am trying to figure out how to use Any operator in Linq.Dynamic, here is my test code:

var bar = new[] {
    new {Bar = "A", Id = 1},
    new {Bar = "B", Id = 1},
    new {Bar = "C", Id = 2},
    new {Bar = "A", Id = 2},
    new {Bar = "B", Id = 3},
    new {Bar = "C", Id = 3}
}; 
var foo = new[] {
    new {Foo = 1, Id = 1},
    new {Foo = 1, Id = 1},
    new {Foo = 2, Id = 2},
    new {Foo = 2, Id = 2},
    new {Foo = 2, Id = 3},
    new {Foo = 3, Id = 3}
}; 
var res = foo.GroupJoin(bar, x => x.Id, y => y.Id, (x, l) => new { F = x.Foo, Bs = l.Select(b => b.Bar).ToArray() }).ToArray();
var normalLinq = res.Where(x => x.Bs.Any(y => y == "A")).ToArray();
var dynamicLinq = res.AsQueryable().Where("Bs.Any(x => x==\"A\")").ToArray(); //won't work

The syntax "Bs.Any(x => x==\"A\")" is wrong as dynamic linq doesn't seem to know the lambda expression I put in, and exception is thrown. How do I make it work?

My goal is to parse dynamic search input with this code. If you Linq experts have other better ways to do dynamic search, please suggest?

Était-ce utile?

La solution

If you are using this library: http://www.nuget.org/packages/System.Linq.Dynamic/ as far as I know it does not allow lambda expressions. However, there is somebody who extended this library. I have never tried it but it looks promising. http://dynamiclinq.codeplex.com/ Good luck :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top