根据选择获取相关实体的简洁方法

发布于 2024-10-09 08:55:29 字数 230 浏览 1 评论 0原文

我正在使用 C# 和 EF 4。我有一个 Actor 存储库。实体 Actor 与 Movie 具有多对多关系。 因此,在 ActorReposity 中,我想要一个方法示例 GetActors,其工资大于 100,并且在结果中我想包含该演员曾出演过的电影,其导演是 Doe。(导演是电影的财产) ) 我在存储库中的方法的签名是 IQueryable GetActors()。在这种情况下,我只是简化签名,在我让它工作之后,我可以重构以传递参数。

I am using C# and EF 4. I have a Actor repository. The entity Actor has many to many relationship with Movie.
So in the ActorReposity i'd like to have a method example GetActors whose salary is greater than 100 and also in the results i would like to include the movies that this actor has been in whose director is Doe.(director is property on the movie)
the signiture of the method i have in the repository is IQueryable GetActors(). In this case i am just simpifying the signiture, After i get this to work then i can refactor to pass in parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-10-16 08:55:29
   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()

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

       }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文