linq到实体的结果很奇怪

时间:2013-10-02 14:25:39

标签: c# mysql linq entity

我正在为我可以设置区域和城市的网站制作一些过滤器,但我只想显示有产品的区域和城市,但城市部分给我错误的结果。我首先在普通的MySql中进行了查询,并且运行正常:

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);

linq to entity查询只返回所有城市而不关心区域过滤器

1 个答案:

答案 0 :(得分: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();