我想为餐馆建一个目录,搜索基于三个条件,我所遇到的问题是添加使用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();
答案 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();