[英]How can I use conditional Order with EntityFramework/linq
每當我有很多選項可以訂購和過濾時,在我的存儲庫中我使用“switch”來決定,但代碼非常重復。
如何使用 coditional order 刪除重復的代碼? 誰有優化此代碼的解決方案?
示例代碼:
IEnumerable<Products> products;
switch (orderBy)
{
case "Create":
products = _GFazContext.Products
//... many conditions like:
.Where(r => (productId == null || r.ProductId == productId))//Duplicate code
.Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
.Where(r => (statusId == null || r.StatusId == statusId)) //Duplicate code
//Condition about the order
.Where(r => (data1 == null || r.CreateDate >= data1))
.Where(r => (data2 == null || r.CreateDate <= data2))
.Include(r => r.Group) //Duplicate code
//Specific order
.OrderBy(p => p.CreateDate)
.Skip(pagination.SkipRecords)//Duplicate code
.Take(pagination.PageSize);//Duplicate code
break;
case "Update":
products = _GFazContext.Products
//... many conditions like:
.Where(r => (productId == null || r.ProductId == productId))//Duplicate code
.Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
.Where(r => (statusId == null || r.StatusId == statusId)) //Duplicate code
//conditions about the order
.Where(r => (data1 == null || r.LastUpdate >= data1))
.Where(r => (data2 == null || r.LastUpdate <= data2))
.Include(r => r.Group) //Duplicate code
//Specific orders
.OrderBy(p => p.LastUpdate)
.Skip(pagination.SkipRecords)//Duplicate code
.Take(pagination.PageSize);//Duplicate code
break; break;
default:
products = _GFazContext.Products
//... many conditions like:
.Where(r => (productId == null || r.ProductId == productId))//Duplicate code
.Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
.Where(r => (statusId == null || r.StatusId == statusId)) //Duplicate code
//Condition about the order
.Where(r => (data1 == null || r.ReleaseDate >= data1))
.Where(r => (data2 == null || r.ReleaseDate <= data2))
.Include(r => r.Group) //Duplicate code
//Specific orders
.OrderBy(p => p.ReleaseDate)
.Skip(pagination.SkipRecords)//Duplicate code
.Take(pagination.PageSize);//Duplicate code
break;
}
return products;
像這樣的東西
IQueryable<Products> products = _GFazContext.Products
.Where(r => (productId == null || r.ProductId == productId))
.Where(r => (groupId == null || r.GroupId == groupId))
.Where(r => (statusId == null || r.StatusId == statusId));
switch (orderBy)
{
case "Create":
products = products
.Where(r => (data1 == null || r.CreateDate >= data1))
.Where(r => (data2 == null || r.CreateDate <= data2))
.Include(r => r.Group)
.OrderBy(p => p.CreateDate);
break;
case "Update":
products = products
.Where(r => (data1 == null || r.LastUpdate >= data1))
.Where(r => (data2 == null || r.LastUpdate <= data2))
.Include(r => r.Group)
.OrderBy(p => p.LastUpdate);
break;
default:
products = products
.Where(r => (data1 == null || r.ReleaseDate >= data1))
.Where(r => (data2 == null || r.ReleaseDate <= data2))
.Include(r => r.Group)
.OrderBy(p => p.ReleaseDate);
break;
}
products = products
.Skip(pagination.SkipRecords)
.Take(pagination.PageSize);
return products;
嘗試這個 -
IEnumerable<Products> products;
products = _GFazContext.Products
.Where(r => (productId == null || r.ProductId == productId))
.Where(r => (groupId == null || r.GroupId == groupId))
.Where(r => (statusId == null || r.StatusId == statusId))
.Where(r => orderBy == "Create" ? (data1 == null || r.CreateDate >= data1) : (orderBy == "Update" ? (data1 == null || r.LastUpdate >= data1) : (data1 == null || r.ReleaseDate >= data1)))
.Where(r => orderBy == "Create" ? (data2 == null || r.CreateDate <= data2) : (orderBy == "Update" ? (data2 == null || r.LastUpdate <= data2) : (data2 == null || r.ReleaseDate <= data2)))
.Include(r => r.Group)
//Specific order
.OrderBy(p => orderBy == "Create" ? p.CreateDate : (orderBy == "Update"? p.LastUpdate : p.ReleaseDate))
.Skip(pagination.SkipRecords)
.Take(pagination.PageSize);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.