帮助Linq查询

109*_*793 3 c# linq sql-server asp.net

我正在写一个linq查询,我遇到了麻烦,所以我想知道是否有人可以提供帮助.这里有一些背景知识:

我没有设计数据库,因此无法更改结构.所以我有Main'Game'表,它有一个主要的产品代码,这个表中的外键是来自GameData表的GameDataID,它包含发布日期等信息,等等.然后我有了GameFormat表,包含每种格式的游戏产品代码,例如Mac,Windows等,GameDataID也是外键.见下文.

Game

GameID PK
MainGameProductCode
MainGameTitle
GameDataID FK

GameData

GameDataID PK
GameReleaseDate
GameReleasedBy

GameFormat

GameFormatID PK
GameDataID FK
GameFormatProductcode
Run Code Online (Sandbox Code Playgroud)

因此,当收到销售报告时,有些只包含'GameFormatProductCode'作为产品标识符.所以从'GameFormatProductCode'我需要在主Game表中检索'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;

}
Run Code Online (Sandbox Code Playgroud)

那里的任何linq专家都在关注我指向正确的方向吗?Linq很新,所以要温柔:)

Geo*_*ord 5

您可以GameDataIDGameFormat表中获取并使用它来查询Game

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.
    var gID = (from t in ctn.Game
               where t.GameDataID == gf.GameDataID
               select t.GameID).FirstOrDefault();

    return gID;

}
Run Code Online (Sandbox Code Playgroud)