Linq具有多个条件的where子句 [英] Linq where clause with multiple conditions

查看:112
本文介绍了Linq具有多个条件的where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法返回通用列表,但它具有多个条件来获得选择. 我只是用if-else if -else if ....这么多if else来写这个 有没有更短的方法可以做到这一点?谢谢.

this method returns generic list but it has multiple condition to get select. I'm just writing this using if - else if -else if.... so many if else i mean Is there a shorter way to do this? Thank you.

    public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate)
    {
        var db = new requestsDBEntities();
        var listPrn = new List<ProductReqNoDate>();
        if (!string.IsNullOrEmpty(departmant))
        {
            return  (from r in db.requests
                       where r.departmant== departmant
                       select new ProductReqNoDate
                       {
                           departmant= r.departmant,
                           reqNo = r.reqNo ,
                           reqDate = r.reqDate ,
                           prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
                       }).ToList();

        }
        if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
        {
            DateTime dtfirstDate = Convert.ToDateTime(firstDate);
            DateTime dtlastDate = Convert.ToDateTime(lastDate);
            return (from r in db.requests
                       where r.reqDate <= dtlastDate && r.reqDate >= dtfirstDate 
                       select new ProductReqNoDate
                       {
                           departmant= r.departmant,
                           reqNo = r.reqNo ,
                           reqDate = r.reqDate,
                           prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
                       }).ToList();

        }
    }

推荐答案

您可以将查询的核心如下:

You can have the core of your query as the following:

var query = from r in db.requests 
select new ProductReqNoDate
                   {
                       departmant= r.departmant,
                       reqNo = r.reqNo ,
                       reqDate = r.reqDate ,
                       prdctName= stringCutter((from p in db.products 
                      where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault())
                   }

然后应用if then else:

if (!string.IsNullOrEmpty(departmant))
    return  query.Where(r=>r.departmant== departmant).ToList();
if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
{
        DateTime dtfirstDate = Convert.ToDateTime(firstDate);
        DateTime dtlastDate = Convert.ToDateTime(lastDate);
        return query.Where(r=> r.reqDate <= dtlastDate && r.reqDate >= dtfirstDate)
                    .ToList();
 }

它不会减少if then else,但会使事情变得更明智.

It doesn't reduce if then else but makes more sensible, what going happens.

这篇关于Linq具有多个条件的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆