Linq 查询帮助
我正在编写一个 linq 查询,但遇到了麻烦,所以我想知道是否有人可以提供帮助。这是一些背景知识:
我没有设计数据库,因此无法更改结构。所以我有主“游戏”表,其中有一个主要产品代码,该表中的外键是 GameData 表中的 GameDataID,其中包含发布日期、发布者等信息。然后我有 GameFormat 表,其中包含每种格式(例如 Mac、Windows 等)游戏的产品代码,并且 GameDataID 是外键。见下文。
Game
GameID PK
MainGameProductCode
MainGameTitle
GameDataID FK
GameData
GameDataID PK
GameReleaseDate
GameReleasedBy
GameFormat
GameFormatID PK
GameDataID FK
GameFormatProductcode
因此,当收到返回的销售报告时,有些报告仅包含“GameFormatProductCode”作为产品标识符。因此,我需要从“GameFormatProductCode”中检索主游戏表中的“GameID”。
到目前为止,我已经编写了 linq 查询来从 GameFormat 表中检索 GameFormatProductcode,但是我不确定如何从主 Game 表中检索 GameID。
private Int64 GetGameID(string gameFormatProductCode)
{
ModelCtn ctn = new ModelCtn();
Game game = null;
GameFormat gf = null;
gf = (from t in ctn.GameFormat
where t.GameFormatProductcode == gameFormatProductCode
select t).FirstOrDefault();
// Need to find GameID from Game table and return it.
return gf;
}
有 linq 专家愿意为我指出正确的方向吗?对 Linq 还很陌生,所以要温柔一点:)
I'm writing a linq query and I've run into trouble with it so I was wondering if someone could help. Here is a bit of background:
I didn't design the database so the structure cannot be changed. So I have the Main 'Game' table, which has a main product code, A foreign key in this table is GameDataID from the GameData table which contains information such as release dates, released by, etc. Then I have the GameFormat table, which contains the product codes for the game in each format, e.g. Mac, Windows, etc and again the GameDataID is a foreign key. See Below.
Game
GameID PK
MainGameProductCode
MainGameTitle
GameDataID FK
GameData
GameDataID PK
GameReleaseDate
GameReleasedBy
GameFormat
GameFormatID PK
GameDataID FK
GameFormatProductcode
So when sales reports are received back, some only contain 'GameFormatProductCode' as the product identifier. So from 'GameFormatProductCode' I need to retrieve the 'GameID' in the main Game table.
So far I have written the linq query to retrieve the GameFormatProductcode from the GameFormat table, however I am unsure how to go about retrieving the GameID from the main Game table.
private Int64 GetGameID(string gameFormatProductCode)
{
ModelCtn ctn = new ModelCtn();
Game game = null;
GameFormat gf = null;
gf = (from t in ctn.GameFormat
where t.GameFormatProductcode == gameFormatProductCode
select t).FirstOrDefault();
// Need to find GameID from Game table and return it.
return gf;
}
Any linq experts out there care to point me in the right direction? Very new to Linq so be gentle :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从
GameFormat
表中获取GameDataID
并使用它来查询Game
表You can get the
GameDataID
from theGameFormat
table and use it to query theGame
table我认为这是可能的唯一方法是是否存在连接 Games 和 GameData 表的一对一关系。如果 Game.GameDataID 只能使用 GameDataID 一次(且只能使用一次),那么这应该可以工作。如果一个 GameData 记录可以被多个游戏使用,则此方法将不可用。
The only way I see it being possible is if there is a one to one relationship connecting Games and GameData tables. If Game.GameDataID can only use a GameDataID once (and only once) then this should work. If a GameData record can be used by multiple Games then this method will not be usable.
来自
GameFormat
的GameDataId
可用于从Game
表中查找GameID
。编辑:
太慢了!杰夫的回答很好。
The
GameDataId
fromGameFormat
could be used to find theGameID
from theGame
table.EDIT:
Way too slow! Good answer by Geoff.