Linq At least one object must implement IComparable

c# entity-framework linq linq-to-entities linq-to-objects

Question

A list of entities that also contains another list of entities is what I'm attempting to order. Even after implementing IComparable for each entity, I continue to receive the exception. All of the examples I've seen deal with the problem of ordering a single list by a certain field, but not when there is a list of lists. This problem affects both Linq to Entities and Linq to Objects, as shown below. What am I overlooking?

[TestClass]
public class OrderBy
{
    [TestMethod]
    public void OrderByTest()
    {
        var hobbies = new Collection<Hobby> { new Hobby { HobbyId = 1, Name = "Eating" }, new Hobby() { HobbyId = 2, Name = "Breathing" } };

        var p1 = new Person
        {
            PersonId = 1,
            Name = "A",
            PersonHobbies = new Collection<PersonHobby> { new PersonHobby() { PersonHobbyId = 1}}
        };
        var p2 = new Person
        {
            PersonId = 2,
            Name = "Z",
            PersonHobbies = new Collection<PersonHobby> { new PersonHobby() { PersonHobbyId = 2 }}
        };

        var people = new List<Person> { p1, p2 };
        var pplEnumerable = people.AsEnumerable();
        pplEnumerable = pplEnumerable.OrderByDescending(r => r.PersonHobbies.OrderByDescending(p => p.Hobby.Name));
        foreach (var person in pplEnumerable)
        {
            Console.WriteLine(person.Name);
        }
    }
    public class Person : IComparable
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<PersonHobby> PersonHobbies { get; set; }
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            var otherPerson = obj as Person;
            return PersonId.CompareTo(otherPerson.PersonId);
        }
    }
    public class PersonHobby : IComparable
    {
        public int PersonHobbyId { get; set; }
        public int HobbyId { get; set; }
        public virtual Person Person{ get; set; }
        public int PersonId { get; set; }
        public virtual Hobby Hobby { get; set; }
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            var otherPersonHobby = obj as PersonHobby;
            return PersonHobbyId.CompareTo(otherPersonHobby.PersonHobbyId);
        }
    }
    public class Hobby : IComparable
    {
        public int HobbyId { get; set; }
        public string Name { get; set; }
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            var otherHobby = obj as Hobby;
            return HobbyId.CompareTo(otherHobby.HobbyId);
        }
    }
}
1
4
4/26/2014 6:42:25 AM

Accepted Answer

Lists do not by default support ordering. To compare lists, you must either create a new class (similar to EquatableList, etc.) or use the LINQ Except & Intersect operators.

However, in light of your point, if you're seeking the LINQ equivalent of:

select * from Person p join PersonHobby ph 
on ph.PersonId = p.PersonId join Hobby h 
on h.HobbyId = ph.HobbyId order by h.Name

then this can be done as follows:

var query = people.SelectMany(p => p.PersonHobbies)
                  .Join(hobbies, ph => ph.HobbyId, h => h.HobbyId, 
                        (ph, h) => new 
                        { 
                         Person = ph.Person, PersonHobby = ph, Hobby = h 
                        })
                  .OrderBy(r => r.Hobby.Name);

In essence, we link the columns for person, person hobbies, and hobby on the keys, project all of the columns, and sort the data by hobby. as stated in your SQL, name field.

6
4/26/2014 8:29:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow