0

I am making some filters to a site where i can set area and city but i only want to show the areas and cities where there is products but the city part is giving me wrong results. I first made the query in plain MySql and that worked fine:

SELECT b.city, COUNT(b.city)
FROM ads AS a 
JOIN locations AS b ON b.id = a.locations_id
JOIN areas AS c ON c.id = b.areas_id
WHERE 
a.title LIKE '% mini' OR
a.title LIKE '% mini %' OR
a.title LIKE 'mini %' AND
c.id = '7ea21f5b-5c75-11e1-8809-4061869766cd'
GROUP BY b.city

Linq to Entities:

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.areas_id equals c.id
             where 
             a.title.Contains(" " + searchQuery) ||
             a.title.Contains(" " + searchQuery + " ") ||
             a.title.Contains(searchQuery + " ") &&
             c.id.Equals(area)
             select new {
                 b.city
             }).OrderBy(x => x.city).GroupBy(x => x.city).ToList();

return jsonOutputString.Serialize(query);

the linq to entity query is just returning all the cities and dont care about the area filter

4

1 回答 1

1

尝试这样的事情

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.areas_id equals c.id
                             where 
                             (a.title.Contains(" " + searchQuery) ||
                             a.title.Contains(" " + searchQuery + " ") ||
                             a.title.Contains(searchQuery + " ")) &&
                             b.areas_id.Equals(area)
                             select new
                                        {
                                            b.city
                                        }).OrderBy(x => x.city).GroupBy(x => x.city).ToList();
于 2013-10-02T14:28:36.087 回答