How to build a nested query with the dynamic LINQ library

.net-3.5 c# dynamic-linq linq

Question

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.

1
4
4/25/2012 11:30:27 AM

Accepted Answer

Try this:

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

or

var userName = "Patrick";
rolesCollection
    .Where("AssignedUsers.Where(Name.FirstName == @0).Any()", userName);
6
12/22/2014 4:36:17 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