>我有这个SQL查询:
select *
from press
where compId = 36
and categories in (1, 7, 21);
我尝试以 ASP.NET MVC存储库模式的格式应用它:
iPressRepository.GetAll().Where(x => (x.compId == 36)).ToList();
但是我的结果不正确,因为我在第二个查询中错过了类别"in"。有没有办法将IN
运算符与存储库模式一起应用?
提前致谢
var categoriesToLookFor = new[] { 1, 7, 21 };
var press = iPressRepository.GetAll()
.Where(x => (x.compId == 36) && (categoriesToLookFor.Contains(x.categories)))
.ToList();
我认为应该是这样的
iPressRepository.GetAll().Where(x => (x.compId == 36)&&(x.categories==1||x.categories==7||x.categories==21)).ToList();
如果你使用GetAll((,你将从 SQL 获取所有数据,但你只需要一些数据,所以你应该使用AsQueryable((
存储 库:
public IQueryable<T> AsQueryable()
{
return _entities.AsQueryable();
}
控制器:
var list = iPressRepository.AsQueryable()
.Where(i => (new[] { 1, 7, 21 }).Contains(i.categories) && i.compId == 36).ToList();