如何在Where子句中使用Condition?
用户可以从列表中选择部分,产品和型号,并查看结果.我See All在所有的文件中添加一个项目,如果用户选择See All,他可以看到所有部分中的所有产品.所以我想写一个查询值,如果每个属性等于-1点引入where条件.
//My Model
struct Model
{
public int SectionCode{get;set;}
public int ProductCode{get;set;}
public int ModelCode{get;set;}
}
var query=DBContext.Model.Where(data=>data.ModelCode==_ModelCode//if ModelCode!=-1
&& ProductCode==_ProductCode//if ProductCode!=-1
&& SectionCode==_SectionCode//if SectionCode!=-1 )
Run Code Online (Sandbox Code Playgroud)
我知道我可以写一些,if但我必须检查很多条件.所以我想知道,如果在Clause的哪个地方怎么写?
如果您不需要,请不要添加where子句,即:
IQueryable<Model> query = DBContext.Model;
if(_ModelCode != -1)
{
query = query.Where(data=>data.ModelCode==_ModelCode);
}
if(_ProductCode!= -1)
{
query = query.Where(data=>data.ProductCode==_ProductCode);
}
if(_SectionCode!= -1)
{
query = query.Where(data=>data.SectionCode==_SectionCode);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5682 次 |
| 最近记录: |