简体   繁体   English

在多对多关系中添加where子句

[英]adding where clauses to a many many relationship

I want to build a directory for restaurant, the search is based in three condition the problem that I have is add the third where clause that uses typecuisine like you see in code the 2 first conditions are good my entity 我想为餐厅建立目录,搜索是基于三个条件的,我typecuisine的问题是添加使用typecuisine的第三个where子句,就像在代码中看到的那样,第2个条件是我的实体

var query = db.Restaurants.AsQueryable();

if (!string.IsNullOrEmpty(Name))
    query = query.Where(c => c.Nom.Contains(Name));

if (RegionId != Guid.Empty)
    query = query.Where(c => c.RegionId == RegionId);

//how to get typecuisine ID from table Restaurant instead it's only in  table TypeCuisine


if (typeId != Guid.Empty)
    query = query.Where(tc=> tc.TypeCuisines.Where(r=>r.TypeCuisineId == typeId));

return query.ToList();

The .Where is expecting a return type of bool and you are returning the results of an inner Where , meaning an IEnumerable . .Where期望返回bool类型,而您返回的是内部Where的结果,即IEnumerable

What you are missing is the .Any() function instead of the inner Where . 您缺少的是.Any()函数,而不是内部Where

var query = db.Restaurants.AsQueryable();

if (!string.IsNullOrEmpty(Name))
    query = query.Where(c => c.Nom.Contains(Name));

if (RegionId != Guid.Empty)
    query = query.Where(c => c.RegionId == RegionId);

if (typeId != Guid.Empty)
    query = query.Where(tc=> tc.TypeCuisines.Any(r => r.TypeCuisineId == typeId));

return query.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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