您好我正在研究一些旧的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);
}
}
我为糟糕的编码道歉,但我没有做代码。代码基本上使用相同的方法过滤产品 该方法的所有参数实际上都是可选的,因此它们都可以为null。所以代码基本上是比较特定参数是否为空。如果它为null,则不要将其包含在Where子句中。
现在,我不想将50多个条件用于非空参数的各种组合,而是希望将此代码转换为单个LINQ调用。
所以我的问题是,如何在Where子句中检查特定参数是否为null,如果它为null,则从Where子句中消除其条件?那些最优化和最少的代码行是什么?
我尝试了什么?
我试图在Where子句本身添加null检查,并试图做这样的事情 -
Where((ProductName!=null && x.ProductName.Contains(ProductName.ToUpper())) && (CompanyName!=null && x.CompanyName.Contains(CompanyName.ToUpper()))
但我不认为这是一个正确的条件,它根本没有给我任何结果。
答案 0 :(得分:2)
您可以在初始声明后向查询添加条件,只是不要在之前调用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();