ASP.NET MVC2简单Linq问题

ASP.NET MVC2简单Linq问题,linq,asp.net-mvc-2,Linq,Asp.net Mvc 2,我有以下代码: public ActionResult ViewCategory(string categoryName, string searchCriteria = "Price") { // Retrieve Category and its associated Listings from the database var categoryModel = db.Categories.Include("Listings")

我有以下代码:

      public ActionResult ViewCategory(string categoryName, string searchCriteria = "Price")
    {
        // Retrieve Category and its associated Listings from the database
        var categoryModel = db.Categories.Include("Listings")
            .Single(c => c.Title == categoryName);

        var viewModel = new ClassifiedsBrowseViewModel
        {
            Category = categoryModel,
            Listings = categoryModel.Listings.ToList()
        };

        return View(viewModel);
    }
此代码返回给定类别中的一些列表

但我想根据某些条件对这些搜索结果重新排序。例如,价格

非常感谢,, J

您想要使用或取决于需求

例如,以最高价格订购-

var viewModel = new ClassifiedsBrowseViewModel
        {
            Category = categoryModel,
            Listings = categoryModel.Listings.OrderByDescending(c=>c.Price).ToList()
        };

另一个可能有效的选项,取决于您呈现信息的方式:使用类似jquery tablesorter插件的东西,并在客户端对结果进行排序


显然,如果有很多结果,并且您正在进行分页,这将不起作用,但是对于表中显示的一页结果,它非常有效。

编写Linq的另一种方法是使用查询语言:

Listings = from item in categoryModel.Listings
           orderby item.Price
           select item;

当您选择单行时,为什么需要执行GroupBy?您尝试按什么进行分组?为什么要包括物品清单?这是你的小组的一部分或不相关?对不起,我已经更新了我的问题,以添加完整的代码,基本上我有一个分类网站。我有不同的类别,在这些类别中我有列表,这个页面只是检索特定类别的列表。。。但是我该如何改变这些列表的顺序呢?您好,谢谢您的建议。我如何根据搜索条件字符串的内容进行搜索?我恐怕不完全理解…但是如果您指的是按字符串值动态排序-使用switch语句,测试您期望的每个字符串,在每种情况下执行不同的查询。。。
Listings = from item in categoryModel.Listings
           orderby item.Price
           select item;