【问题标题】:Unsure how to generate Linq for my query SQL不确定如何为我的查询 SQL 生成 Linq
【发布时间】:2014-03-12 12:30:23
【问题描述】:

我有以下表格: 表定义:

季节(ID、描述、开始日期)
游戏(ID、SeasonID、描述、日期、类型、发布、玩家计数)
结果(ID、PlayerID、GameID、位置、积分)
玩家(ID、姓氏、名字等)

我想知道一名球员参加了哪些赛季。有人能指出我使用 EF 获取我的 linq 查询的正确方向吗?我遇到了路障。我很确定我需要在某处使用 Distinct() 但就是想不通。

【问题讨论】:

  • 你能发布你的表格定义吗?
  • 是的,当然... 赛季(ID、描述、开始日期) - 游戏(ID、SeasonID、描述、日期、类型、发布、玩家计数) - 结果(ID、玩家ID、游戏ID、位置、点数) ) - 玩家(ID、姓氏、名字等)。谢谢。

标签: sql asp.net-mvc linq


【解决方案1】:

您应该能够获取Season 对象的列表并调用Distinct

int playerID = {something};

var seasons = (from s in db.Seasons
               join g in db.Games on s.ID equals g.SeasonID
               join r in db.Results on g.ID equals r.GameID
               where r.PlayerID == playerID
               select s
              ).Distinct();

【讨论】:

  • 感谢您的帮助。那只是门票!
【解决方案2】:

只需要将您的表连接在一起(对表定义做一些假设):

from s in seasons
join g in games on s.Id equals g.seasonId
join r in results on g.Id equals r.gameId
join p in players on r.Id equals p.resultId
where p.Id == playerIdYouCareAbout
select s.whatever

这将使您了解季节,如果需要,您可以从那里使用Distinct

【讨论】:

  • 感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多