在许多关系中添加where子句

时间:2016-08-05 06:09:24

标签: c# linq lambda many-to-many entity

我想为餐馆建一个目录,搜索基于三个条件,我所遇到的问题是添加使用typecuisine的第三个where子句,就像你在代码中看到的那样,第一个条件是好的{{ 3}}

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

1 个答案:

答案 0 :(得分:0)

.Where预计返回类型为bool,您将返回内部Where的结果,即IEnumerable

您缺少的是.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();