【发布时间】:2017-07-12 21:59:06
【问题描述】:
我有一些业务逻辑供 api 和使用数据库存储数据的作业管理器使用。像这样简化:。
class BusinessLogic
{
public IEnumerable<Request> Requests {get;set;}
public List<Requests> GetData()
{
return Requests.Where(r => r.StatusId == Constants.STATUS_NOT_PROCESSED_ID).ToList();
}
}
class apiprocessor
{
public void Process()
{
var requests = new List<Request>();
BusinessLogic.Requests = requests;
BusinessLogic.GetData();
}
}
class dbprocessor
{
private DbContext _db;
public void Process()
{
//this sends the where clause to the db
//var requests = _db.Requests.Where(r => r.StatusId == Constants.STATUS_NOT_PROCESSED_ID).ToList();
BusinessLogic.Requests = _db.Requests; //type DbSet<Request> Requests
//this one doesnt
BusinessLogic.GetData();
}
}
这在功能上可行,但有一个问题。
如果我尝试使用 db 处理器中的 dbcontext 获取数据,mysql 服务器收到的结果查询是:
SELECT
`Extent1`.`RequestID`,
`Extent1`.`RequestType`,
`Extent1`.`StatusId`
FROM `Requests` AS `Extent1`
WHERE (0 = `Extent1`.`StatusId`)
(注意 WHERE 子句)
当在上面的 BusinessLogic 类中运行相同的代码时,得到的查询是:
SELECT
`Extent1`.`RequestID`,
`Extent1`.`RequestType`,
`Extent1`.`StatusId`
FROM `Requests` AS `Extent1`
缺少 Where 子句,这意味着正在检索整个表,然后将 where 子句应用于内存中的数据
无论如何设计公共类,以便在使用 dbset 调用它时将 where 子句发送到 db?
编辑:需要明确的是,IQueryable 不是一个选项,因为 List 不会从它继承,这是 api 处理器使用的。但是,在 api 处理器中使用 List 不是强制性的
谢谢!
【问题讨论】: