我的数据模型由 4 个表组成:项目引用产品、运输和用户。我想按产品字段对项目进行排序:类别和条件。在我的应用程序中,我使用 LINQ To SQL 类。这是我的代码:
public ViewResult Content(string category, string condition, int page = 1)
{
var Items = _itemsRepository.GetItems()
.Where(i => category == null && condition == null ||
(i.Product.Category != null && i.Product.Category == category) ||
(i.Product.Condition != null && i.Product.Condition == condition))
.OrderBy(i => i.ItemId)
.Skip((page - 1) * PageSize)
.Take(PageSize)
.Select(i => i);
// return Items to the view
}
问题也可能是由路由引起的,所以这里是 global.asax 内容:
// without categories and conditions
routes.MapRoute(
null,
"Store/Content/Page{page}",
new { controller = "Store", action = "Content", category = (string)null, condition = (string)null },
new { page = @"\d+" });
// with conditions
routes.MapRoute(
null,
"Store/Content/{condition}/Page{page}",
new { controller = "Store", action = "Content", category = (string)null },
new { page = @"\d+" });
// with categories
routes.MapRoute(
null,
"Store/Content/{category}/Page{page}",
new { controller = "Store", action = "Content", condition = (string)null },
new { page = @"\d+" });
// Default route
routes.MapRoute(
null,
"{controller}/{action}",
new { controller = "Home", action = "Index"});
如果我将带有类别的路线放在带有条件的路线之上,它可以按类别排序,反之亦然。如何同时按类别和条件排序?谢谢。