IQueryable 不在 SQL 中附加 WHERE 语句
IQueryable does not append WHERE statement in SQL
我在 MySQL 服务器上使用 EF6。我正在尝试根据变量是否为 null 动态附加 WHERE 子句。
这是我的代码:
using (var dbContext = new Entities())
{
IQueryable<Boxes> boxes = dbContext.Boxes;
if(this.Customer != null)
boxes.Where(box => box.CurrentCustomer == this.Customer);
if(this.IDs != null)
boxes.Where(box => this.IDs.Split(',').Any(id => id == box.ID.ToString()));
return new Response() { Success = true, Result = boxes.ToList() };
}
但是,WHERE 子句未过滤数据,table 中的所有行都被返回。同样在 MySQL 日志中,我看到了不包含 WHERE 子句的语句:
1994 Query SELECT
`Extent1`.`ID`,
`Extent1`.`CurrentCustomer`
FROM `Boxes` AS `Extent1`
我使用的IQueryable
错了吗?
调用Where
方法时需要保存查询:
IQueryable<Boxes> boxes = dbContext.Boxes;
if(this.Customer != null)
boxes= boxes.Where(box => box.CurrentCustomer == this.Customer);
if(this.IDs != null)
boxes=boxes.Where(box => this.IDs.Split(',').Any(id => id == box.ID.ToString()));
//...
我在 MySQL 服务器上使用 EF6。我正在尝试根据变量是否为 null 动态附加 WHERE 子句。
这是我的代码:
using (var dbContext = new Entities())
{
IQueryable<Boxes> boxes = dbContext.Boxes;
if(this.Customer != null)
boxes.Where(box => box.CurrentCustomer == this.Customer);
if(this.IDs != null)
boxes.Where(box => this.IDs.Split(',').Any(id => id == box.ID.ToString()));
return new Response() { Success = true, Result = boxes.ToList() };
}
但是,WHERE 子句未过滤数据,table 中的所有行都被返回。同样在 MySQL 日志中,我看到了不包含 WHERE 子句的语句:
1994 Query SELECT
`Extent1`.`ID`,
`Extent1`.`CurrentCustomer`
FROM `Boxes` AS `Extent1`
我使用的IQueryable
错了吗?
调用Where
方法时需要保存查询:
IQueryable<Boxes> boxes = dbContext.Boxes;
if(this.Customer != null)
boxes= boxes.Where(box => box.CurrentCustomer == this.Customer);
if(this.IDs != null)
boxes=boxes.Where(box => this.IDs.Split(',').Any(id => id == box.ID.ToString()));
//...