LINQ operation with two inner joins

Actually, I think I could fix it, gona do some tests, I will post my solution if it works

======================================== Hi guys

I am migrating an older DB system to LINQ, I am having problems converting multiple SQL statements, but

SELECT * FROM cities 
INNER JOIN deals ON cities.cityId = deals.CityID 
INNER JOIN countries ON cities.countryID = countries.CountryId 
WHERE deals.endDate >= (someDate) 
   AND countries.CountryId = (1) 
   AND deals.soldout = (false)

I did some research, but I can't get it to work, here is the LINQ expression that I came up with,

var deals = from d in db.deals
                     join city in db.cities on d.CityID equals city.cityId
                     join country in db.countries on city.countryID equals country.CountryId
                     where d.endDate > DateTime.Today && country.CountryId == 1 && d.soldOut == false
                     select d;

Is there any specific way to use 2 joins in LINQ?

Greetings

Sorry, I had a formatting error,

The application is designed to select all transactions that have a city, where countryID = 1

+3
source share
2 answers

You do not need a second connection if you have a country code for the city ...

var deals = from d in db.deals
                 join city in db.cities on d.CityID equals city.cityId
                 where d.endDate > DateTime.Today &&
                 city.CountryId == 1 && d.soldOut == false
                 select d;
+2
source

If you need columns from all the tables involved, you can use an anonymous type for this.

select new {d, city, country}
0
source

All Articles