首先将此 SQL 转换为 EF 4 的 lambda 代码
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,首先,如果您想确保在运行查询时所有内容都已急切加载,您应该添加显式
Include
:但是,正如我在评论中提到的,这不会'不会产生与 SQL 查询相同的结果,因为您没有从子集合中过滤掉玩家。
EF 4.1 有一些漂亮的 显式加载相关实体时应用过滤器功能,但我无法让它适用于子子集合,所以我认为最接近原始查询的方法是将结果投影到匿名对象上(或者,如果您稍后需要传递该对象,则可以为其创建一个类):
然后您可以枚举并检查结果:
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
: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):
Then you can enumerate and inspect the results:
我确实找到了解决方案。
如果有人有更好的解决方案,那么我会洗耳恭听。
I did found the solution.
If somebody has a better solution then I am all ears..