如何建立与动态LINQ库中的嵌套查询 [英] How to build a nested query with the dynamic LINQ library

查看:116
本文介绍了如何建立与动态LINQ库中的嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何建立以下LINQ查询与动态的LINQ库(System.Linq.Dynamic)?

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和AssignedUsers是其实现IEnumerable接口的集合。

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");

但是,这并不正常工作。一个ParseException的消息没有适用的聚合方法,去哪儿存在被抛出。

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

在此先感谢。

推荐答案

试试这个:

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

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

这篇关于如何建立与动态LINQ库中的嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆