Search code examples
c#asp.netlinqentity-framework-4sql-order-by

List OrderBy Issue


I am tring to handle a gridview sort on my own. I can't figure this one out:

protected void gvPackages_Sorting(object sender, GridViewSortEventArgs e)
    {
    List<Package> pck = new List<Package>();
    pck = Session["Packages"] as List<Package>;

    var output = (from p in pck
                  orderby p.Department
                  select p);        <-line that breaks
    // var output = pck.OrderBy(x => x.Department).ToList(); <- I've also tried this
    gvPackages.DataSource = output;
    gvPackages.DataBind();
    }

The error I'm getting is:

At least one object must implement IComparable.

Package is an entity, so I don't understand what I'm missing. Other examples I'm finding on the web reflect what I have above, and various iterations of it. What am I missing? Let me also say that Department is just one field as a test to get it to work, it will be more dynamic in the final version.

EDIT: Thanks again stack, I'm a big dummy.


Solution

  • the problem there is that you are trying to order on department, but the department type doesnt know how it should be ordered because you havent implemented IComparable which requires methods to specify order, e.g. Orderby Dept.Name or Dept.Location, Dept.Size, who knows?