Linq query help

I am writing a linq request and I had problems with this, so I was wondering if anyone could help. Here is some background:

I did not design the database, so the structure cannot be changed. Thus, I have a table "Main game" in which there is a main product code. The foreign key in this table is the GameDataID from the GameData table, which contains information such as release dates, released, etc. Then I have a GameFormat table that contains the product codes for the game in each format, for example Mac, Windows, etc. Again, 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

, , "GameFormatProductCode" . "GameFormatProductCode" "GameID" .

linq GameFormatProduct GameFormat, , 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 :)

+3
3

GameDataID GameFormat 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;

}
+5

, , , - , 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();
+3

GameDataIdof GameFormatcan be used to search GameIDfrom a table Game.

EDIT: Too slow! Good answer Jeff.

+1
source

Source: https://habr.com/ru/post/1793826/


All Articles