tiistai 27. joulukuuta 2016

ASP.NET MVC5 - Linq kyselyn Inner join syntaksi

Välillä tarvitsen SQL-kyselyjä, joissa yhdistetään eri taulujen tietoja. Tässä INNER JOIN kyselyn syntaksi Linq-koodilla:


var results = from c in db.Companies
              join cn in db.Countries on c.CountryID equals cn.ID
              join ct in db.Cities on c.CityID equals ct.ID
              join sect in db.Sectors on c.SectorID equals sect.ID
              where (c.CountryID == cn.ID) && (c.CityID == ct.ID) && (c.SectorID == company.SectorID) && (company.SectorID == sect.ID)
              select new { country = cn.Name, city = ct.Name, c.ID, c.Name, c.Address1, c.Address2, c.Address3, c.CountryID, c.CityID, c.Region, c.PostCode, c.Telephone, c.Website, c.SectorID, Status = (ContactStatus)c.StatusID, sector = sect.Name };


return results.ToList();

Ei kommentteja:

Lähetä kommentti