How to build a nested query with the dynamic LINQ library

落爺英雄遲暮 提交于 2019-12-01 04:56:52

问题


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:


Try this:

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

or

var userName = "Patrick";
rolesCollection
    .Where("AssignedUsers.Where(Name.FirstName == @0).Any()", userName);


来源:https://stackoverflow.com/questions/10314708/how-to-build-a-nested-query-with-the-dynamic-linq-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!