如何使用 lambda 表达式查询嵌套列表
在我的存储库实现中,我可以使用 lambda 表达式运行以下查询:
public IList<User> GetUsersFromCountry(string)
{
return _UserRepository.Where(x => x.Country == "Sweden").ToList();
}
到目前为止,一切都很好,很简单。但是,我在针对嵌套 -> 编写 lambda 表达式时遇到困难。嵌套列表。给出以下示例(抱歉想不出更好的示例):
以下查询工作得非常好,并返回所有会员年龄超过 45 岁的俱乐部
public IList<Clubs> GetGoldMembers()
{
var clubs = from c in ClubRepository
from m in c.Memberships
where m.User.Age > 45
select c;
return clubs;
}
目前,我对 lambda 表达式的了解就到此为止了。
我如何使用 lambda 表达式针对 ClubRepository 编写上述查询,类似于上面的示例?
In my repository implementation I can run the following query using a lambda expression:
public IList<User> GetUsersFromCountry(string)
{
return _UserRepository.Where(x => x.Country == "Sweden").ToList();
}
So far so good, simple stuff. However, I'm having difficulties to write a lambda expression against a nested -> nested list. Given the following example (sorry couldn't think of a better one):
The following query works absolutely fine and returns all clubs, which have members over the age of 45
public IList<Clubs> GetGoldMembers()
{
var clubs = from c in ClubRepository
from m in c.Memberships
where m.User.Age > 45
select c;
return clubs;
}
At the moment, this is where my knowledge of lambda expression ends.
How could I write the above query against the ClubRepository, using a lambda expression, similar to the example above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能有效(未经测试)...
This might work (untested)...
这是一种方法:
Here's one way to do it:
更通用的方式
NestedList2 匹配“列表”的数据类型
A more generic way
Where NestedList2 matches the data type of "list"