检查Linq where子句是否为空
本文关键字:是否 子句 Linq where 检查 | 更新日期: 2023-09-27 18:14:26
嗨,我正在处理一些旧的ASP代码。Net MVC控制器。这里有一个包含1500行代码的控制器方法。当我检查的时候,我知道有超过50+ LINQ查询从数据库中获取,根据一个特定的字段是否为空。
为了更好地理解它,下面是这个特殊方法的样子-
public ActionResult MyProductList(string Msg, string ProductName, string CompanyName, string DivisionName, string Type, string Form, string Packing, int page = 1, int pageSize = 20){
if (CompanyName == null)
{
CompanyName = "";
}
if (ProductName == null)
{
ProductName = "";
}
if (DivisionName == null)
{
DivisionName = "";
}
if (Type == null)
{
Type = "";
}
if (Form == null)
{
Form = "";
}
if (Packing == null)
{
Packing = "";
}
if (CompanyName != "")
{
var query1 = (from mypdtlist in db.MyProductlist
join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
where mypdtlist.User_ID.Equals(UserID)
select new ViewModal
{
ProductName = pdt.ProductName,
CompanyName = pdt.CompanyName,
DivisionName = pdt.DivisionName,
Type = pdt.Type,
Form = pdt.Form,
Packing = pdt.Packing,
MRP = pdt.MRP,
DrugName = pdt.DrugName,
ID = mypdtlist.ID
}).Where(x => x.CompanyName.Contains(CompanyName.ToUpper())).Take(200).ToList();
ViewBag.CompanyName = CompanyName;
// list = query.Take(100).Where(x => x.CompanyName.Contains(CompanyName.ToUpper())).ToList();
PagedList<ViewModal> model1 = new PagedList<ViewModal>(query1, page, pageSize);
CookieManager.AddToViewBag(HttpContext, ViewBag);
return View(model1);
}
if (ProductName != "")
{
ViewBag.ProductName = ProductName;
var query2 = (from mypdtlist in db.MyProductlist
join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
where mypdtlist.User_ID.Equals(UserID)
select new ViewModal
{
ProductName = pdt.ProductName,
CompanyName = pdt.CompanyName,
DivisionName = pdt.DivisionName,
Type = pdt.Type,
Form = pdt.Form,
Packing = pdt.Packing,
MRP = pdt.MRP,
DrugName = pdt.DrugName,
ID = mypdtlist.ID
}).Where(x => x.ProductName.Contains(ProductName.ToUpper())).Take(200).ToList();
//list = query.Take(100).Where(x => x.ProductName.Contains(ProductName.ToUpper())).ToList();
PagedList<ViewModal> model1 = new PagedList<ViewModal>(query2, page, pageSize);
CookieManager.AddToViewBag(HttpContext, ViewBag);
return View(model1);
}
//.... After a lot more similar condition checks and LINQ queries
else if (ProductName != "" && CompanyName != "" && DivisionName != "" && Type != "" && Form != "" && Packing != "")
{
ViewBag.ProductName = ProductName;
ViewBag.CompanyName = CompanyName;
ViewBag.DivisionName = DivisionName;
ViewBag.Type = Type;
ViewBag.Form = Form;
ViewBag.Packing = Packing;
var query51 = (from mypdtlist in db.MyProductlist
join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
where mypdtlist.User_ID.Equals(UserID)
select new ViewModal
{
ProductName = pdt.ProductName,
CompanyName = pdt.CompanyName,
DivisionName = pdt.DivisionName,
Type = pdt.Type,
Form = pdt.Form,
Packing = pdt.Packing,
MRP = pdt.MRP,
DrugName = pdt.DrugName,
ID = mypdtlist.ID
}).Where(x => x.ProductName.Contains(ProductName.ToUpper()) && x.CompanyName.Contains(CompanyName.ToUpper()) && x.DivisionName.Contains(DivisionName.ToUpper()) && x.Type.Contains(Type.ToUpper()) && x.Form.Contains(Form.ToUpper()) && x.Packing.Contains(Packing.ToUpper())).Take(300).ToList();
PagedList<ViewModal> model = new PagedList<ViewModal>(query51, page, pageSize);
// List<Product> pdt = db.Product.Where(x => x.ProductName.Contains(ProductName.ToUpper()) && x.CompanyName.Contains(CompanyName.ToUpper()) && x.DivisionName.Contains(DivisionName.ToUpper()) && x.Type.Contains(Type.ToUpper()) && x.Form.Contains(Form.ToUpper()) && x.Packing.Contains(Packing.ToUpper())).Take(300).ToList();
// PagedList<Product> model = new PagedList<Product>(pdt, page, pageSize);
//var list = db.Product.Take(20).ToList();
return View(model);
}
}
我为糟糕的代码道歉,但代码不是我写的。代码基本上使用了相同的方法来过滤产品。
该方法的所有参数实际上都是可选的,因此它们都可以为空。代码基本上是比较特定参数是否为空。如果它为空,则不要将其包含在Where子句中。
现在,我想将此代码转换为单个LINQ调用,而不是为非空参数的各种组合使用50多个条件。
所以我的问题是,我如何检查一个特定的参数在Where子句中是否为空,如果它是空的,然后从Where子句中消除它的条件?要做到这一点,最优化和最少行代码是什么?
我做了什么?
我试图在Where子句本身添加空检查,并试图做这样的事情-
Where((ProductName!=null && x.ProductName.Contains(ProductName.ToUpper())) && (CompanyName!=null && x.CompanyName.Contains(CompanyName.ToUpper()))
但是我不认为这是一个正确的条件,它根本没有给我任何结果。
您可以在初始声明之后向查询添加where条件,只是不要在此之前调用ToList()。
所以第一部分是:
var query1 = (from mypdtlist in db.MyProductlist
join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
where mypdtlist.User_ID.Equals(UserID)
select new ViewModal
{
ProductName = pdt.ProductName,
CompanyName = pdt.CompanyName,
DivisionName = pdt.DivisionName,
Type = pdt.Type,
Form = pdt.Form,
Packing = pdt.Packing,
MRP = pdt.MRP,
DrugName = pdt.DrugName,
ID = mypdtlist.ID
})
现在你可以像这样添加更多的条件:
if (!string.IsNullOrWhiteSpace(ProductName))
query1 = query1.Where(x => x.ProductName.Contains(ProductName.ToUpper()));
当所有条件都完成后,只需返回ToList():
return query1.ToList();