我正在使用C#和EF 4.我有一个Actor存储库。实体Actor与Movie有很多关系。 所以在ActorReposity中我想要一个方法示例GetActors,其薪水大于100,并且在结果中我想包括这个演员在其导演是Doe的电影。(导演是电影中的属性) ) 我在存储库中的方法的签名是IQueryable GetActors()。在这种情况下,我只是简化了签名,在我得到这个工作后,我可以重构传递参数。
答案 0 :(得分:0)
public class ActorRepository
{
public List<Actor> GetActors(int minimumSalary, string directorName)
{
return _ctx.Actors
.Include("Movies")
.Where(x => x.Salary > minimumSalary)
.Select(x => new
{
Actor = x,
Movies = x.Movies.Where(y => y.DirectorName == directorName)
})
.ToList()
.Select(x => new Actor
{
// copy scalar properties from anon to Actor, e.g:
ActorName = x.ActorName,
// then set the Movies navigational property to the filtered type
Movies = x.Movies
}).ToList()
}
}