具有多个条件的 Linq where 子句

发布于 2024-12-25 17:51:14 字数 1702 浏览 1 评论 0原文

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

    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();

        }
    }

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();

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七分※倦醒 2025-01-01 17:51:14

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

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 的数量,而是使发生的情况变得更加合理。

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())
                   }

Then apply 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();
 }

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

白芷 2025-01-01 17:51:14

1*

你可以找到更好的解决方案,但我希望这个帮助(我的事情),但我使用它:你可以测试这个功能

   List<ProductReqNoDate> yourList = GetRequestsQuery(string departmant, int reqStateID)

    if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
    {
      yourdatagrid.Itemsource = yourList.where(a=> a.reqDate <= Datetime.parse(firstDate) & a.reqDate >= Datetime.parse(lastDate))
    }


public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID)
{
    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();

    }
}

,你可以应用第一个列表中的任何条件,但不建议在 havy 应用程序或许多信息中使用数据库。

2*

或者您可以只指定第一个日期和最后一个日期;例如,如果未设置日期,则使第一个日期 = 01/01/1900 和最后一个日期 Datetime.Today 并始终在 linq 查询中传递您的日期

1*

you can found better sollution but I wish this help (I thing) but I use it : you can make the test out this function

   List<ProductReqNoDate> yourList = GetRequestsQuery(string departmant, int reqStateID)

    if (!string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate))
    {
      yourdatagrid.Itemsource = yourList.where(a=> a.reqDate <= Datetime.parse(firstDate) & a.reqDate >= Datetime.parse(lastDate))
    }


public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID)
{
    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();

    }
}

and you can apply any condition in your first list but in not recommended in havy application or many information in database.

2*

Or you can just make the first date and the last date; for exemple if the date is not set,make the first date = 01/01/1900 and the last date Datetime.Today and always pass your date in the linq query

治碍 2025-01-01 17:51:14

像这样的东西我还没有编译和检查过,但它应该给你一个线索。

public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate)
 {
    using(var db = new requestsDBEntities())
    {

      DateTime dtfirstDate =null;
      DateTime.TryParse(firstDate,out dtfirstDate);

      DateTime dtlastDate = null;
      DateTime.TryParse(lastDate,out dtlastDate);

      var result = (from r in db.requests
                   where 
                      (r.departmant == departmant)
                   || (r.reqDate <= dtlastDate.Value && r.reqDate >= dtfirstDate.Value)
                   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();
    }

 }

编辑:

如果你想在你的过滤器中使用非sql函数,那么在你的表上调用ToList(),

 var result = db.requests.ToList().Where(r => { 

     // do test for what you want
     // so write a function to work out when you want to filter by
     // name or date
     return true;

 }).Select( r => new ProductReqNoDate
                   {
                       departmant = r.departmant,
                       reqNo = r.reqNo ,
                       reqDate = r.reqDate ,
                       prdctName= stringCutter(db.products.Where(p=> p.reqNo == r.reqNo).Select(p=> p.prdctName).FirstOrDefault())
                   })

问题是你正在将整个表加载到内存中,如果你不经常更改其中的值的话,这很好。如果不是太大的话。

另一种方法是将其编写为存储过程,这可能会让您的 DBA 满意。

something like this I haven't compiled and checked it but it should give you a clue.

public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate)
 {
    using(var db = new requestsDBEntities())
    {

      DateTime dtfirstDate =null;
      DateTime.TryParse(firstDate,out dtfirstDate);

      DateTime dtlastDate = null;
      DateTime.TryParse(lastDate,out dtlastDate);

      var result = (from r in db.requests
                   where 
                      (r.departmant == departmant)
                   || (r.reqDate <= dtlastDate.Value && r.reqDate >= dtfirstDate.Value)
                   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();
    }

 }

EDIT:

if you want to use non-sql functions in your filter then call ToList() on your table

 var result = db.requests.ToList().Where(r => { 

     // do test for what you want
     // so write a function to work out when you want to filter by
     // name or date
     return true;

 }).Select( r => new ProductReqNoDate
                   {
                       departmant = r.departmant,
                       reqNo = r.reqNo ,
                       reqDate = r.reqDate ,
                       prdctName= stringCutter(db.products.Where(p=> p.reqNo == r.reqNo).Select(p=> p.prdctName).FirstOrDefault())
                   })

the problem with this is that you are loading the whole table into memory which if fine if you don't change to values in it often and if it's not too big.

Another approach would be to write it as a stored proc which would probably make your DBA happy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文