In my repository implementation I can run the following query using a lambda expression:
在我的存储库实现中,我可以使用lambda表达式运行以下查询:
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):
到目前为止,这么好,简单的东西。但是,我很难针对嵌套 - >嵌套列表编写lambda表达式。鉴于以下示例(抱歉无法想到更好的一个):
The following query works absolutely fine and returns all clubs, which have members over the age of 45
以下查询绝对正常,并返回所有俱乐部,其成员年龄超过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.
目前,这是我对lambda表达的知识结束的地方。
How could I write the above query against the ClubRepository, using a lambda expression, similar to the example above?
我如何使用lambda表达式对ClubRepository编写上述查询,类似于上面的示例?
2 个解决方案
#1
24
This might work (untested)...
这可能有用(未经测试)......
var clubs = ClubRepository.Where(c=>c.MemberShips.Any(m=>m.User.Age > 45));
#2
15
Here's one way to do it:
这是一种方法:
var clubs = clubRepository
.SelectMany(c => c.Memberships, (c, m) => new { c, m })
.Where(x => x.m.User.Age > 45)
.Select(x => x.c);
#1
24
This might work (untested)...
这可能有用(未经测试)......
var clubs = ClubRepository.Where(c=>c.MemberShips.Any(m=>m.User.Age > 45));
#2
15
Here's one way to do it:
这是一种方法:
var clubs = clubRepository
.SelectMany(c => c.Memberships, (c, m) => new { c, m })
.Where(x => x.m.User.Age > 45)
.Select(x => x.c);