LINQ 到实体中的奇怪结果

本文关键字:结果 实体 LINQ | 更新日期: 2023-09-27 18:33:39

我正在对一个可以设置区域和城市的网站进行一些过滤器,但我只想显示有产品的地区和城市,但城市部分给了我错误的结果。我首先在普通的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 到实体查询只返回所有城市,而不关心区域筛选器

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