Linq 查询帮助

发布于 2024-10-18 16:31:09 字数 1077 浏览 2 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(3

愛上了 2024-10-25 16:31:09

您可以从 GameFormat 表中获取 GameDataID 并使用它来查询 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;

}

You can get the GameDataID from the GameFormat table and use it to query the 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.
    var gID = (from t in ctn.Game
               where t.GameDataID == gf.GameDataID
               select t.GameID).FirstOrDefault();

    return gID;

}
永言不败 2024-10-25 16:31:09

我认为这是可能的唯一方法是是否存在连接 Games 和 GameData 表的一对一关系。如果 Game.GameDataID 只能使用 GameDataID 一次(且只能使用一次),那么这应该可以工作。如果一个 GameData 记录可以被多个游戏使用,则此方法将不可用。

gf = (from t in ctn.GameFormat
     join g in ctn.Game on t.Game equals gd.GameDataID
     where t.GameFormatProductcode == gameFormatProductCode
     select g.GameID).FirstOrDefault();

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.

gf = (from t in ctn.GameFormat
     join g in ctn.Game on t.Game equals gd.GameDataID
     where t.GameFormatProductcode == gameFormatProductCode
     select g.GameID).FirstOrDefault();
誰ツ都不明白 2024-10-25 16:31:09

来自 GameFormatGameDataId 可用于从 Game 表中查找 GameID

编辑:
太慢了!杰夫的回答很好。

The GameDataId from GameFormat could be used to find the GameID from the Game table.

EDIT:
Way too slow! Good answer by Geoff.

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