首先将此 SQL 转换为 EF 4 的 lambda 代码

发布于 2024-11-07 10:54:18 字数 747 浏览 0 评论 0原文

我有这个 Sql 语句

SELECT * FROM Game 
        INNER JOIN Series ON Series.Id = Game.SeriesId 
        INNER JOIN SeriesTeams ON SeriesTeams.SeriesId = Series.Id 
        INNER JOIN Team ON Team.Id = SeriesTeams.TeamId 
        INNER JOIN TeamPlayers ON TeamPlayers.TeamId = Team.Id 
        INNER JOIN Player ON Player.Id = TeamPlayers.PlayerId 
    WHERE AND Game.StartTime >= GETDATE() 
        AND Player.Id = 1

,我想将其转换为 lambda 表达式。

这就是它的工作原理。

一场比赛只能加入1个系列赛,但一个系列赛当然可以有很多场比赛。一个系列赛可以有很多支球队,一个球队也可以参加很多个系列赛。 一名球员可以在多支球队效力,而一支球队也有多名球员。

SeriesTeams 和 TeamPlayers 只是 EF 创建的多对多表,用于保存系列/团队和团队/球员之间的引用。

提前致谢...

编辑:我使用 EF 4 CTP5,希望得到 lambda 形式的答案函数,或者在 linq 中,如果这更容易的话......

I have this Sql statement

SELECT * FROM Game 
        INNER JOIN Series ON Series.Id = Game.SeriesId 
        INNER JOIN SeriesTeams ON SeriesTeams.SeriesId = Series.Id 
        INNER JOIN Team ON Team.Id = SeriesTeams.TeamId 
        INNER JOIN TeamPlayers ON TeamPlayers.TeamId = Team.Id 
        INNER JOIN Player ON Player.Id = TeamPlayers.PlayerId 
    WHERE AND Game.StartTime >= GETDATE() 
        AND Player.Id = 1

That I want to be converted into a lambda expression.

This is how it works.

A game can only be joined to 1 series, but a serie can of course have many games. A serie can have many teams and a team can join many series.
A player can play in many teams and a team has many players.

SeriesTeams and TeamPlayers are only the many-to-many tables created by EF to hold the references between series/teams and Teams/Players

Thanks in advance...

Edit: I use the EF 4 CTP5 and would like to have the answer as lambda functions, or in linq if that is easier...

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

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

发布评论

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

评论(2

还给你自由 2024-11-14 10:54:18

好的,首先,如果您想确保在运行查询时所有内容都已急切加载,您应该添加显式 Include

context.
Games.
Include(g => g.Series.Teams.Select(t => t.Players)).
Where(g => 
         g.StartTime >= DateTime.Now && 
         g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))).
ToList();

但是,正如我在评论中提到的,这不会'不会产生与 SQL 查询相同的结果,因为您没有从子集合中过滤掉玩家。

EF 4.1 有一些漂亮的 显式加载相关实体时应用过滤器功能,但我无法让它适用于子子集合,所以我认为最接近原始查询的方法是将结果投影到匿名对象上(或者,如果您稍后需要传递该对象,则可以为其创建一个类):

var query = context.
            Games.
            Where(g =>
                     g.StartTime >= DateTime.Now && 
                     g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))).
            Select(g => new
                        {
                            Game = g,
                            Players = g.
                                      Series.
                                      Teams.
                                      SelectMany(t => t.
                                                      Players.
                                                      Where(p => p.Id == user.Id))
                        });

然后您可以枚举并检查结果:

var gamesAndPlayersList = query.ToList();

Ok, first of all, if you want to make sure that everything is eager-loaded when you run your query, you should add an explicit Include:

context.
Games.
Include(g => g.Series.Teams.Select(t => t.Players)).
Where(g => 
         g.StartTime >= DateTime.Now && 
         g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))).
ToList();

However, as I mentioned in my comment, this won't produce the same results as your SQL query, since you don't filter out the players from the child collection.

EF 4.1 has some nifty Applying filters when explicitly loading related entities features, but I couldn't get it to work for sub-sub-collections, so I think the closest you can get to your original query would be by projecting the results onto an anonymous object (or you can create a class for that if you need to pass this object around later on):

var query = context.
            Games.
            Where(g =>
                     g.StartTime >= DateTime.Now && 
                     g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))).
            Select(g => new
                        {
                            Game = g,
                            Players = g.
                                      Series.
                                      Teams.
                                      SelectMany(t => t.
                                                      Players.
                                                      Where(p => p.Id == user.Id))
                        });

Then you can enumerate and inspect the results:

var gamesAndPlayersList = query.ToList();
善良天后 2024-11-14 10:54:18

我确实找到了解决方案。

IList<Domain.Model.Games> commingGames = this.Games
 .Where(a => a.StartTime >= DateTime.Now && a.Series.Teams.Any(t => t.Players.Any(p => p.Id == user.Id))).ToList();

如果有人有更好的解决方案,那么我会洗耳恭听。

I did found the solution.

IList<Domain.Model.Games> commingGames = this.Games
 .Where(a => a.StartTime >= DateTime.Now && a.Series.Teams.Any(t => t.Players.Any(p => p.Id == user.Id))).ToList();

If somebody has a better solution then I am all ears..

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