Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多条件Linq扩展 我需要考虑多个条件来获得价值。 我的意思是,如果所有条件都为真,那么必须给我一个过滤的答案。 或者其中一个是真的,其余的是假的。。。 所以我需要写下所有的可能性?? 如果(a&b&c&d)else如果(a&b&c)else如果(a&c&d)else如果(b&c&d)else如果(a&b)else如果(a&c)…等等??)有没有较短的方法可以做到这一点 public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName) { var db = new requestDBEntities(); bool dp = !string.IsNullOrEmpty(departmant); bool pr = !string.IsNullOrEmpty(productName); bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate); bool rs = reqStateID > 0 ? true : false; var query = (from r in db.requests select new ProductReqNoDate { departmant = r.departmant, reqNo = r.reqNo, reqDate = r.reqDate, productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault() }).ToList(); if (dp & pr & tm & rs) { var rState = (from ta in db.reqStates where ta.reqStateID == reqStateID && ta.isActive == true select ta.reqNo).ToList(); var prName = (from p in db.products where p.productName.Contains(productName) select p.reqNo).ToList(); DateTime dtfirstDate = Convert.ToDateTime(firstDate); DateTime dtlastDate = Convert.ToDateTime(lastDate); return query.Where( r => rState.Contains(r.reqNo) //find by Request State && r.departmant == departmant //find by Departmant && (r.reqDate >= dtfirstDate && r.reqDate <= dtlastDate) //find by Date && prName.Contains(r.reqNo) //Find By Product Name ).ToList(); } else if (dp & pr & tm) { /*return query.Where(...} */} else if (pr & tm & rs) { /*return query.Where(...} */} else if (dp & pr && rs) { /*return query.Where(...} */} else if (dp & pr) { /*return query.Where(...} */} //else if ...etc } public List GetRequestsQuery(字符串departmant、int-reqStateID、字符串firstDate、字符串lastDate、字符串productName) { var db=new requestDBEntities(); bool dp=!string.IsNullOrEmpty(departmant); bool pr=!string.IsNullOrEmpty(productName); bool tm=!string.IsNullOrEmpty(firstDate)和&!string.IsNullOrEmpty(lastDate); bool rs=reqStateID>0?真:假; var query=(来自db.requests中的r) 选择新ProductReqNode { departmant=r.departmant, 需求编号=r.需求编号, reqDate=r.reqDate, productName=(从db.products中的p开始,其中p.reqNo==r.reqNo选择p.productName) }).ToList(); if(dp&pr&tm&rs) { var rState=(从db.reqStates中的ta开始) 其中ta.reqStateID==reqStateID&&ta.isActive==true 选择ta.reqNo.ToList(); var prName=(从p.productName.Contains所在的db.products中的p选择p.reqNo.ToList(); DateTime dtfirstDate=Convert.ToDateTime(firstDate); DateTime dtlastDate=Convert.ToDateTime(lastDate); 返回查询。在哪里( r=>rState.Contains(r.reqNo)//按请求状态查找 &&r.departmant==departmant//find by departmant &&(r.reqDate>=dtfirstDate&&r.reqDate_Linq_Conditional Statements - Fatal编程技术网

多条件Linq扩展 我需要考虑多个条件来获得价值。 我的意思是,如果所有条件都为真,那么必须给我一个过滤的答案。 或者其中一个是真的,其余的是假的。。。 所以我需要写下所有的可能性?? 如果(a&b&c&d)else如果(a&b&c)else如果(a&c&d)else如果(b&c&d)else如果(a&b)else如果(a&c)…等等??)有没有较短的方法可以做到这一点 public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName) { var db = new requestDBEntities(); bool dp = !string.IsNullOrEmpty(departmant); bool pr = !string.IsNullOrEmpty(productName); bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate); bool rs = reqStateID > 0 ? true : false; var query = (from r in db.requests select new ProductReqNoDate { departmant = r.departmant, reqNo = r.reqNo, reqDate = r.reqDate, productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault() }).ToList(); if (dp & pr & tm & rs) { var rState = (from ta in db.reqStates where ta.reqStateID == reqStateID && ta.isActive == true select ta.reqNo).ToList(); var prName = (from p in db.products where p.productName.Contains(productName) select p.reqNo).ToList(); DateTime dtfirstDate = Convert.ToDateTime(firstDate); DateTime dtlastDate = Convert.ToDateTime(lastDate); return query.Where( r => rState.Contains(r.reqNo) //find by Request State && r.departmant == departmant //find by Departmant && (r.reqDate >= dtfirstDate && r.reqDate <= dtlastDate) //find by Date && prName.Contains(r.reqNo) //Find By Product Name ).ToList(); } else if (dp & pr & tm) { /*return query.Where(...} */} else if (pr & tm & rs) { /*return query.Where(...} */} else if (dp & pr && rs) { /*return query.Where(...} */} else if (dp & pr) { /*return query.Where(...} */} //else if ...etc } public List GetRequestsQuery(字符串departmant、int-reqStateID、字符串firstDate、字符串lastDate、字符串productName) { var db=new requestDBEntities(); bool dp=!string.IsNullOrEmpty(departmant); bool pr=!string.IsNullOrEmpty(productName); bool tm=!string.IsNullOrEmpty(firstDate)和&!string.IsNullOrEmpty(lastDate); bool rs=reqStateID>0?真:假; var query=(来自db.requests中的r) 选择新ProductReqNode { departmant=r.departmant, 需求编号=r.需求编号, reqDate=r.reqDate, productName=(从db.products中的p开始,其中p.reqNo==r.reqNo选择p.productName) }).ToList(); if(dp&pr&tm&rs) { var rState=(从db.reqStates中的ta开始) 其中ta.reqStateID==reqStateID&&ta.isActive==true 选择ta.reqNo.ToList(); var prName=(从p.productName.Contains所在的db.products中的p选择p.reqNo.ToList(); DateTime dtfirstDate=Convert.ToDateTime(firstDate); DateTime dtlastDate=Convert.ToDateTime(lastDate); 返回查询。在哪里( r=>rState.Contains(r.reqNo)//按请求状态查找 &&r.departmant==departmant//find by departmant &&(r.reqDate>=dtfirstDate&&r.reqDate

多条件Linq扩展 我需要考虑多个条件来获得价值。 我的意思是,如果所有条件都为真,那么必须给我一个过滤的答案。 或者其中一个是真的,其余的是假的。。。 所以我需要写下所有的可能性?? 如果(a&b&c&d)else如果(a&b&c)else如果(a&c&d)else如果(b&c&d)else如果(a&b)else如果(a&c)…等等??)有没有较短的方法可以做到这一点 public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName) { var db = new requestDBEntities(); bool dp = !string.IsNullOrEmpty(departmant); bool pr = !string.IsNullOrEmpty(productName); bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate); bool rs = reqStateID > 0 ? true : false; var query = (from r in db.requests select new ProductReqNoDate { departmant = r.departmant, reqNo = r.reqNo, reqDate = r.reqDate, productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault() }).ToList(); if (dp & pr & tm & rs) { var rState = (from ta in db.reqStates where ta.reqStateID == reqStateID && ta.isActive == true select ta.reqNo).ToList(); var prName = (from p in db.products where p.productName.Contains(productName) select p.reqNo).ToList(); DateTime dtfirstDate = Convert.ToDateTime(firstDate); DateTime dtlastDate = Convert.ToDateTime(lastDate); return query.Where( r => rState.Contains(r.reqNo) //find by Request State && r.departmant == departmant //find by Departmant && (r.reqDate >= dtfirstDate && r.reqDate <= dtlastDate) //find by Date && prName.Contains(r.reqNo) //Find By Product Name ).ToList(); } else if (dp & pr & tm) { /*return query.Where(...} */} else if (pr & tm & rs) { /*return query.Where(...} */} else if (dp & pr && rs) { /*return query.Where(...} */} else if (dp & pr) { /*return query.Where(...} */} //else if ...etc } public List GetRequestsQuery(字符串departmant、int-reqStateID、字符串firstDate、字符串lastDate、字符串productName) { var db=new requestDBEntities(); bool dp=!string.IsNullOrEmpty(departmant); bool pr=!string.IsNullOrEmpty(productName); bool tm=!string.IsNullOrEmpty(firstDate)和&!string.IsNullOrEmpty(lastDate); bool rs=reqStateID>0?真:假; var query=(来自db.requests中的r) 选择新ProductReqNode { departmant=r.departmant, 需求编号=r.需求编号, reqDate=r.reqDate, productName=(从db.products中的p开始,其中p.reqNo==r.reqNo选择p.productName) }).ToList(); if(dp&pr&tm&rs) { var rState=(从db.reqStates中的ta开始) 其中ta.reqStateID==reqStateID&&ta.isActive==true 选择ta.reqNo.ToList(); var prName=(从p.productName.Contains所在的db.products中的p选择p.reqNo.ToList(); DateTime dtfirstDate=Convert.ToDateTime(firstDate); DateTime dtlastDate=Convert.ToDateTime(lastDate); 返回查询。在哪里( r=>rState.Contains(r.reqNo)//按请求状态查找 &&r.departmant==departmant//find by departmant &&(r.reqDate>=dtfirstDate&&r.reqDate,linq,conditional-statements,Linq,Conditional Statements,您可以使用以下内容构建表达式: 私有表达式GetPredicate(FilterDto filter) { 表达式谓词=r=>r.ID==r.ID; if(filter.Department.HasValue) 谓词=谓词和(r=>r.Department==filter.Department.Value); if(filter.FirstDate.HasValue) predicate=predicate.And(r=>(r.reqDate>=filter.FirstDate.Value));

您可以使用以下内容构建表达式:

私有表达式GetPredicate(FilterDto filter)
{
表达式谓词=r=>r.ID==r.ID;
if(filter.Department.HasValue)
谓词=谓词和(r=>r.Department==filter.Department.Value);
if(filter.FirstDate.HasValue)
predicate=predicate.And(r=>(r.reqDate>=filter.FirstDate.Value));
if(filter.LastDate.HasValue)
predicate=predicate.And(r=>(r.reqDate r.ID==r.ID
)在这里作为第一个表达式,只是为了有一个表达式作为开头。这个代码段并没有完全涵盖您的代码,但我认为作为一个示例就足够了


使用
返回查询中的表达式。其中(expression).ToList()

您可以使用以下内容构建表达式:

私有表达式GetPredicate(FilterDto filter)
{
表达式谓词=r=>r.ID==r.ID;
if(filter.Department.HasValue)
谓词=谓词和(r=>r.Department==filter.Department.Value);
if(filter.FirstDate.HasValue)
predicate=predicate.And(r=>(r.reqDate>=filter.FirstDate.Value));
if(filter.LastDate.HasValue)
predicate=predicate.And(r=>(r.reqDate r.ID==r.ID
)在这里作为第一个表达式,只是为了有一个表达式作为开头。这个代码段并没有完全涵盖您的代码,但我认为作为一个示例就足够了


使用
返回查询中的表达式。Where(expression).ToList()

每次只需添加一个Where条件

public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName)
{
    var db = new requestDBEntities();
    bool dp = !string.IsNullOrEmpty(departmant);
    bool pr = !string.IsNullOrEmpty(productName);
    bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate);
    bool rs = reqStateID > 0 ? true : false;

    var query = (from r in db.requests
                 select new ProductReqNoDate
                 {
                     departmant = r.departmant,
                     reqNo = r.reqNo,
                     reqDate = r.reqDate,
                     productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault()
                 }).AsQueryable();  //AsQueryable is not always needed, but it shouldn't hurt and I don't feel like checking for this example.


    if (dp)
    {
        query = query.Where(q => /*condition*/);
    }

    if (pr)
    {
        query = query.Where(q => /*condition*/);
    }

    if (tm)
    {
        query = query.Where(q => /*condition*/);
    }

    if (rs)
    {
        query = query.Where(q => /*condition*/);
    }

    return query.ToList();
}
public List GetRequestsQuery(字符串departmant、int-reqStateID、字符串firstDate、字符串lastDate、字符串productName)
{
var db=new requestDBEntities();
bool dp=!string.IsNullOrEmpty(departmant);
bool pr=!string.IsNullOrEmpty(productName);
bool tm=!string.IsNullOrEmpty(firstDate)和&!string.IsNullOrEmpty(lastDate);
bool rs=reqStateID>0?真:假;
var query=(来自db.requests中的r)
选择新ProductReqNode
{
departmant=r.departmant,
需求编号=r.需求编号,
reqDate=r.reqDate,
productName=(从db.products中的p开始,其中p.reqNo==r.reqNo选择p.productName)
}).AsQueryable();//AsQueryable并不总是需要的,但它应该不会有什么坏处,我不想检查这个示例。
如果(dp)
{
query=query.Where(q=>/*条件*/);
}
国际单项体育联合会(公共关系)
{
query=query.Where(q=>/*条件*/);
}
if(tm)
{
query=query.Where(q=>/*条件*/);
}
如果(卢比)
{
query=query.Where(q=>/*条件*/);
}
返回query.ToList();
}

只需一次添加一个Where条件

public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName)
{
    var db = new requestDBEntities();
    bool dp = !string.IsNullOrEmpty(departmant);
    bool pr = !string.IsNullOrEmpty(productName);
    bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate);
    bool rs = reqStateID > 0 ? true : false;

    var query = (from r in db.requests
                 select new ProductReqNoDate
                 {
                     departmant = r.departmant,
                     reqNo = r.reqNo,
                     reqDate = r.reqDate,
                     productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault()
                 }).AsQueryable();  //AsQueryable is not always needed, but it shouldn't hurt and I don't feel like checking for this example.


    if (dp)
    {
        query = query.Where(q => /*condition*/);
    }

    if (pr)
    {
        query = query.Where(q => /*condition*/);
    }

    if (tm)
    {
        query = query.Where(q => /*condition*/);
    }

    if (rs)
    {
        query = query.Where(q => /*condition*/);
    }

    return query.ToList();
}
public List GetRequestsQuery(字符串departmant、int-reqStateID、字符串firstDate、字符串lastDate、字符串productName)
{
var db=new requestDBEntities();
bool dp=!string.IsNullOrEmpty(departmant);
bool pr=!string.IsNullOrEmpty(productName);
bool tm=!string.IsNullOrEmpty(firstDate)和&!string.IsNullOrEmpty(lastDate);
bool rs=reqStateID>0?真:假;
var query=(来自db.requests中的r)
选择新ProductReqNode
{
departmant=r.departmant,
需求编号=r.需求编号,
reqDate=r.reqDate,
productName=(从db.products中的p开始,其中p.reqNo==r.reqNo选择p.productName)
}).AsQueryable();//AsQueryable并不总是需要的,但它应该不会有什么坏处,我不想检查这个示例。
如果(dp)
{
query=query.Where(q=>/*条件*/);
}
国际单项体育联合会(公共关系)
{
query=query.Where(q=>/*条件*/);
}
if(tm)
{
query=query.Where(q=>/*条件*/);
}
如果(卢比)
{
query=query.Where(q=>/*条件*/);
}
返回query.ToList();
}

这不是先从主表中取出100万项,然后在应用程序中应用过滤器吗?…或者如果我们要