I want to get the smallest value in a collection and I am using LINQ to do this. I've read online I can use Min() but I am getting the error message:
At least one object must implement IComparable
This is my code
public virtual void GetBestValue(ModelEnergyCalculator ModelEnergyCalculator)
{
ModelTariffQuote ModelTariffQuote = (from q in ModelEnergyCalculator.ModelTariffQuotes
select q).Min();
}
This is my collection
- - ModelEnergyCalculator {EnergyHelpline.Model.ModelEnergyCalculator} EnergyHelpline.Model.ModelEnergyCalculator
- ModelTariffQuotes Count = 4 System.Collections.Generic.List<EnergyHelpline.Model.ModelTariffQuote>
- [0] {EnergyHelpline.Model.ModelTariffQuote} EnergyHelpline.Model.ModelTariffQuote
ElectricityUsage 0 decimal
FinalElectricityCost 179.97655091937073972602739726 decimal
FinalGasCost 112.48534432460671232876712328 decimal
GasUsage 0 decimal
InitialElectricityCost 30.0117245403146958904109589 decimal
InitialGasCost 18.757327837696684931506849312 decimal
+ QuoteIssuedDate {01/01/0001 00:00:00} System.DateTime
TariffName null string
TotalCalculatedCost 341.23094762198883287671232875 decimal
+ [1] {EnergyHelpline.Model.ModelTariffQuote} EnergyHelpline.Model.ModelTariffQuote
+ [2] {EnergyHelpline.Model.ModelTariffQuote} EnergyHelpline.Model.ModelTariffQuote
+ [3] {EnergyHelpline.Model.ModelTariffQuote} EnergyHelpline.Model.ModelTariffQuote
+ Raw View
+ ModelTariffs Count = 4 System.Collections.Generic.List<EnergyHelpline.Model.ModelTariff>
How do I fix this error or is there a better way for getting the smallest value?
You should tell the compiler how to compare your objects by implementing the
IComparable<T>
interface for your class.Or if you want to smallest value of a property or field you can specity it in your query like this:Or you can use overloaded version of
Min
:The computer doesn't know how to compare a
ModelTariffQuote
with anotherModelTariffQuote
. So it can't find the minimum object. You have to define this comparison by implementing the IComparable interface on yourModelTariffQuote
class. You can also implement the genericIComparable<T>
interface.To use
.Min()
on a collection of a complex value you must implement theIComparable
interface and specify how an object x is greater or equals to an object y. Otherwise there is no way for c# to know how to compare your values.Alternatively you could use this
yourCollection.Select(x => x.ThePropertyIWantToUseToCompare).Min()
will return the smallest ThePropertyIWantToUseToCompare value from all the items in you collection.An easier and more flexible way of doing this than using IComparable is to order the objects as you need and take the first one. If you need the highest then you just need to use OrderByDescending.