Search code examples
c#entity-frameworkentity-framework-4entity-framework-5entity-framework-4.1

Entity Framework Join 3 Tables


I'm trying to join three tables but I can't understand the method...

I completed join 2 tables

        var entryPoint = dbContext.tbl_EntryPoint
            .Join(dbContext.tbl_Entry,
                c => c.EID,
                cm => cm.EID,
                (c, cm) => new
                {
                    UID = cm.OwnerUID,
                    TID = cm.TID,
                    EID = c.EID,
                }).
            Where(a => a.UID == user.UID).Take(10);

tables

I would like to include tbl_Title table with TID PK and get Title field.

Thanks a lot


Solution

  • This is untested, but I believe the syntax should work for a lambda query. As you join more tables with this syntax you have to drill further down into the new objects to reach the values you want to manipulate.

    var fullEntries = dbContext.tbl_EntryPoint
        .Join(
            dbContext.tbl_Entry,
            entryPoint => entryPoint.EID,
            entry => entry.EID,
            (entryPoint, entry) => new { entryPoint, entry }
        )
        .Join(
            dbContext.tbl_Title,
            combinedEntry => combinedEntry.entry.TID,
            title => title.TID,
            (combinedEntry, title) => new 
            {
                UID = combinedEntry.entry.OwnerUID,
                TID = combinedEntry.entry.TID,
                EID = combinedEntry.entryPoint.EID,
                Title = title.Title
            }
        )
        .Where(fullEntry => fullEntry.UID == user.UID)
        .Take(10);