简体   繁体   中英

LINQ to Entities multiple join

Hi im trying to replicate this mysql query

SELECT a.id, a.title, a.description, a.categories_id, c.name, d.title
FROM ads AS a
INNER JOIN locations AS b 
  ON a.locations_id = b.id
INNER JOIN areas AS c 
  ON b.areas_id = c.id
INNER JOIN categories AS d
  ON a.categories_id = d.id
WHERE a.title LIKE '%mini%' 
  AND c.name = 'Fyn'
LIMIT 10

and here is it in LINQ

var query = (from a in db.ads
  join b in db.locations on a.locations_id equals b.id
  join c in db.areas on b.id equals c.id
  join d in db.categories on a.categories_id equals d.id
  where a.title.Contains(searchQuery) && c.name.Equals(area)
  select new {
    a.id,
    a.title,
    a.description,
    category = d.title
}).Take(10);

it doesn't show any error but it's not returning any data.

You have mistake in third line:

join c in db.areas on b.id equals c.id

should be

join c in db.areas on b.areas_id equals c.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM