如何使用Lambda表达式查询嵌套列表 [英] How to query a nested list using a lambda expression

查看:568
本文介绍了如何使用Lambda表达式查询嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的存储库实现中,我可以使用lambda表达式运行以下查询:

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();                  
}

到目前为止,还不错,很简单.但是,我很难针对嵌套->嵌套列表编写lambda表达式.给出以下示例(对不起,想不到更好的示例):

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):

以下查询绝对可以正常工作,并返回会员年龄超过45岁的所有俱乐部

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;
        }

此刻,我对lambda表达的了解到此结束.

At the moment, this is where my knowledge of lambda expression ends.

如何使用类似于上面的示例的lambda表达式针对ClubRepository编写上述查询?

How could I write the above query against the ClubRepository, using a lambda expression, similar to the example above?

推荐答案

这可能有效(未经测试)...

This might work (untested)...

var clubs = ClubRepository.Where(c=>c.MemberShips.Any(m=>m.User.Age > 45));

这篇关于如何使用Lambda表达式查询嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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