具有多个条件的 Linq where 子句
此方法返回通用列表,但它有多个条件来获取选择。 我只是用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将查询的核心设置如下:
然后应用
if then else
:它不会减少
if then else
的数量,而是使发生的情况变得更加合理。You can have the core of your query as the following:
Then apply
if then else
:It doesn't reduce
if then else
but makes more sensible, what going happens.1*
你可以找到更好的解决方案,但我希望这个帮助(我的事情),但我使用它:你可以测试这个功能
,你可以应用第一个列表中的任何条件,但不建议在 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
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
像这样的东西我还没有编译和检查过,但它应该给你一个线索。
编辑:
如果你想在你的过滤器中使用非sql函数,那么在你的表上调用ToList(),
问题是你正在将整个表加载到内存中,如果你不经常更改其中的值的话,这很好。如果不是太大的话。
另一种方法是将其编写为存储过程,这可能会让您的 DBA 满意。
something like this I haven't compiled and checked it but it should give you a clue.
EDIT:
if you want to use non-sql functions in your filter then call ToList() on your table
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.