Dynamic Linq Any syntax

dynamic-linq linq

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?

1
0
1/28/2014 11:18:08 AM

Accepted Answer

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 :)

2
1/28/2014 12:09:54 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow