4

How can I build the following LINQ query with the Dynamic Linq library (System.Linq.Dynamic)?

var roles = rolesCollection.Where(r => r.AssignedUsers.Where(u => u.Name.FirstName == "Patrick").Count() > 0);

rolesCollection and AssignedUsers are collections which implement the IEnumerable interface.

I was thinking about doing something like this:

rolesCollection.Where("AssignedUsers.Where(\"Name.FirstName == 'Patrick'\").Count() > 0");

But that doesn't work. A ParseException with the message "No applicable aggregate method 'Where' exists" is thrown.

Thanks in advance.

Patrick Koorevaar
  • 1,219
  • 15
  • 24

1 Answers1

7

Try this:

rolesCollection
    .Where("AssignedUsers.Where(Name.FirstName == \"Patrick\").Any()");

or

var userName = "Patrick";
rolesCollection
    .Where("AssignedUsers.Where(Name.FirstName == @0).Any()", userName);
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • Dude you just saved me ! Cant figure out this linq and the docs aren't really complete. Where can i learn more about its queries ? – Ariel Apr 06 '16 at 18:02
  • 1
    Check out the docs+example code at http://system-linq-dynamic-core.azurewebsites.net/html/a5ecbe15-aade-af7f-4860-6c5c412a34b4.htm – Stef Heyenrath Jun 14 '16 at 16:17