Alejandro A
asked on
At least one object must implement IComparable. - Sorting a GroupedCollection with Linq
I have the following structures as example:
class ServicesGroup : IServicesGroup, IComparable
{
public string CityName { get; set; }
public ObservableCollection<Servi ce> Services { get; set; }
public int CompareTo(object obj)
{
ServicesGroup sg = (ServicesGroup)obj;
return String.Compare(this.CityNa me, sg.CityName);
}
}
I am trying to sort my collection of Services at each group by its [ServiceId] (ascending and descending):
tempCollection = new ObservableCollection<objec t>(collect ion.OrderB y(x => ((ServicesGroup)x).Service s.OrderBy( y => y.ServiceId)));
My Service class implements IComparable as well:
I tried both
public class Service : IComparable, IEquatable
public class Service : IComparable<Service>, IEquatable<Service>
Example of my Service class:
public class Service : IComparable, IEquatable
{
public string ServiceId {get; set;}
public string ServiceType {get; set;}
public int CompareTo(object obj)
{
Service s = (Service)obj;
return String.Compare(this.Servic eId, s.ServiceId);
}
public bool Equals(Service other)
{
if (this.ServiceId.Equals(oth er.Service Id) && this.ServiceType.Equals(ot her.Servic eType)) return true;
return false;
}
}
When I try to run my linq I get the following error:
Additional information: At least one object must implement IComparable.
I can not access the [public ObservableCollection<Servi ce> Services] for some reason. What am I missing here?
class ServicesGroup : IServicesGroup, IComparable
{
public string CityName { get; set; }
public ObservableCollection<Servi
public int CompareTo(object obj)
{
ServicesGroup sg = (ServicesGroup)obj;
return String.Compare(this.CityNa
}
}
I am trying to sort my collection of Services at each group by its [ServiceId] (ascending and descending):
tempCollection = new ObservableCollection<objec
My Service class implements IComparable as well:
I tried both
public class Service : IComparable, IEquatable
public class Service : IComparable<Service>, IEquatable<Service>
Example of my Service class:
public class Service : IComparable, IEquatable
{
public string ServiceId {get; set;}
public string ServiceType {get; set;}
public int CompareTo(object obj)
{
Service s = (Service)obj;
return String.Compare(this.Servic
}
public bool Equals(Service other)
{
if (this.ServiceId.Equals(oth
return false;
}
}
When I try to run my linq I get the following error:
Additional information: At least one object must implement IComparable.
I can not access the [public ObservableCollection<Servi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER