Double internal join in linq / lambda request?

I have this SQL query that I want to convert to Linq. this is a summary:

im creating an asp.net api that should return values ​​from three different tables

CREATE TABLE Locatie (
   locatieId            INT IDENTITY(1,1)    not null,
   postcode             VARCHAR(10)          not null,
   huisnummer           INT                  not null,
   adres                VARCHAR(50)          not null,
   plaats               VARCHAR(50)          not null,




CREATE TABLE Vereniging (
   verenigingId         INT IDENTITY(1,1)    not null,
   locatieId            INT                  not null,
   naam                 VARCHAR(50)          not null,
   facebookGroupId      BIGINT               null,


CREATE TABLE Saldo (
   saldoId              INT IDENTITY(1,1)    not null,
   lidId                INT                  not null,
   verenigingId         INT                  not null,
   bedrag               SMALLMONEY           not null,

I left all foreign keys and initial. This is just to find out what I want. My problem is that I have a function that should return information from multiple tables. The sql query looks like this:

Select v.verenigingId, l.postcode, l.huisnummer, l.adres,l.plaats,v.naam,v.facebookGroupId 
from Vereniging v inner join Saldo s
on v.verenigingId = s.verenigingId
inner join Locatie l
on v.locatieId=l.locatieId
where s.lidId = 1;

I get all the "verenigingen" from lidid = 1 and show all the information that "verenigingen" has in the Location table.

But when I try to do this using linq / lambda, this does not work correctly; my function looks like this:

public class LibraryRepository : ILibraryRepository
{
    private LibraryContext _context;

    public LibraryRepository(LibraryContext context)
    {
        _context = context;
    }

    public bool Save()
    {
        return (_context.SaveChanges() >= 0);
    }

    public IEnumerable<Verenigingmodel> GetVerenigingenperLid(int lidId)
    {
        return _context.Vereniging
            .Join(
                _context.Saldo.Where(b => b.lidId == lidId),
            ver => ver.verenigingId,
                sal => sal.verenigingId,
                (ver, sal) => new Viewmodel { Vereniging = ver, Saldo = sal })
            .Join(
                _context.Locatie,
                verr => verr.Vereniging.locatieId,
                loca => loca.locatieId,
                (vr, loca) => new Viewmodel { Locatie = loca });
                //this returns wrong sql information
    }

}

my verenigingmodel looks like this:

public class Verenigingmodel
{
    public int verenigingId { get; set; }
    public string postcode { get; set; }
    public int huisnummer { get; set; }
    public string adres { get; set; }
    public string plaats { get; set; }
    public string naam { get; set; }
    public int facebookGroupId { get; set; }
}

my library context is as follows:

public class LibraryContext : DbContext
{
    public LibraryContext(DbContextOptions<LibraryContext> options)
       : base(options)
    {
        Database.Migrate();
    }

    public DbSet<Gebruiker> Gebruiker { get; set; }
    public DbSet<Lid> Lid { get; set; }
    public DbSet<Vereniging> Vereniging { get; set; }
    public DbSet<Saldo> Saldo { get; set; }
    public DbSet<Locatie> Locatie { get; set; }
}

, , , verenigingmodel api:

    [HttpGet("api/Vereniging/{lidId}")]

    public IActionResult FindVereniGingenPerLid(int lidId)
    {
        var verenigingFromRepo = vlibraryRepository.GetVerenigingenperLid(lidId);

        return new JsonResult(verenigingFromRepo);

    }
+4
2

. :

public IEnumerable<Verenigingmodel> GetVerenigingenperLid(int lidId)
{
    return (
        from v in _context.Vereniging
        join s in _context.Saldo
            on v.verenigingId equals s.verenigingId
        join l in _context.Locatie
            on v.locatieId equals l.locatieId
        select new Verenigingmodel()
        {
            verenigingId= v.verenigingId,
            postcode=l.postcode,
            huisnummer=l.huisnummer,
            adres=l.adres,
            naam=v.naam,
            facebookGroupId=v.facebookGroupId,
            plaats=l.plaats
        }
     ).ToList();
}

, ,

+3

, sql, :

return _context.Vereniging
    .Join(
        _context.Saldo.Where(b => b.lidId == lidId),
        v => v.verenigingId,
        s => s.verenigingId,
        (v, s) => new { Vereniging = v, Saldo = s })
    .Join(
        _context.Locatie,
        v => v.Vereniging.locatieId,
        l => l.locatieId,
        (v, l) => 
        new Verenigingmodel 
        {
            verenigingId = v.Vereniging.id,
            postcode = l.postcode,
            huisnummer = l.huisnummer,
            adres = l.adres,
            plaats = l.plaats,
            naam = v.Vereniging.naam,
            facebookGroupId = v.Vereniging.facebookGroupId
        });

linq, . , , , ..

+1

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


All Articles