【问题标题】:System.ArgumentException: DbSortClause expressions must have a type that is order comparable. Parameter name: keySystem.ArgumentException:DbSortClause 表达式必须具有顺序可比的类型。参数名称:key
【发布时间】:2014-01-28 07:35:47
【问题描述】:

我正在尝试按照此页面 (http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-search) 中有关如何创建带有网站下拉列表的搜索页面的说明进行操作。但似乎无法获得下拉列表。它一直给我这个错误:

System.ArgumentException:DbSortClause 表达式必须具有顺序可比的类型。参数名称:key

问题是:CountryList.AddRange(CountryQry.Distinct());

搜索控制器 :::

    public ActionResult Index(string location, string searchString)
    {

        var CountryList = new List<Country>();

        var CountryQry = from d in db.Stuffs
                         orderby d.Country
                         select d.Country; 

        CountryList.AddRange(CountryQry.Distinct());
        ViewBag.location = new SelectList(CountryList);

        var stuff = from m in db.Stuffs
                     select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            stuff = stuff.Where(s => s.stuffName.Contains(searchString));
        }

        if (!String.IsNullOrEmpty(location))
        {
            stuff = stuff.Where(x => x.Country.countryName == location);
        }


        return View(stuff); 
    }

查看 :::

    <form>
         @using (Html.BeginForm("Index","Search",FormMethod.Get)){ 
              @Html.TextBox("SearchString", new { placeholder = "text" }) 
              @Html.DropDownList("location", "All")      
         }
    </form>

Model :::(这是从数据库自动生成的)

public partial class Stuff
{
    public string stuffId { get; set; }
    public string stuffName { get; set; }
    public string countryId { get; set; }

    public virtual Country Country { get; set; }
}

我的 c# 知识非常有限,因此,我希望有人可以帮助我。 任何帮助表示赞赏!谢谢!

【问题讨论】:

    标签: c# drop-down-menu asp.net-mvc-5


    【解决方案1】:

    错误告诉您Distinct 方法需要一个比较Country 对象的标准。你的Country 类是一个复杂的类,它可能有几个属性并且没有实现IComparer 接口。该接口声明了一个比较两个对象的方法,Distinct 方法使用它来判断两个对象是否“相等”。

    您应该在Country 类中实现IComparer/IComparable 接口。

    假设您的 Country 类具有相似的结构(关于属性),您可以执行以下操作(根据名称比较两个国家/地区,但您可以轻松更改比较属性):

    public class Country : IComparer
    {
         public string Name { get; set; }
         public string Capital { get; set; }
         public int Population { get; set; }
    
         int IComparer.Compare(object a, object b)
         {
            Country c1=(Country )a;
            Country c2=(Country )b;
    
            if (c1.Name < c2.Name )
               return 1;
    
            if (c1.Name > c2.Name )
               return -1;
    
            else
               return 0;
         }
    }
    

    编辑:可能需要IEqualityComparer 接口而不是IComparer

    另一个编辑:解决所有这些问题的一种方法是使用:

    var uniqueCountries = CountryQry.GroupBy(c => c.Name).Select(g => g.FirstOrDefault());
    

    【讨论】:

    • 使用“CountryQry”不起作用。但是如果我用“db.Countries”代替它,它就可以工作......它只是不绑定“Stuff”和“Country”。至少它不会给我一个错误。感谢您的帮助!
    • 当涉及到 Linq to Entity 时,此答案不起作用。 IComparer 和 IEqualityComparer 实现返回相同的错误。您需要指定对象的底层属性。
    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多