An extension method on a collection named MeasurementCollection checks if the property Template.Frequency (Enum) of each item has the same value.
public static bool IsQuantized(this MeasurementCollection items)
{
return (from i in items
select i.Template.Frequency)
.Distinct()
.Count() == 1;
}
edit info about underlying classes
MeasurementCollection : ICollection<IMeasurement>
IMeasurement
{
IMeasurementTemplate Template { get; }
......
}
Is this a correct approach or is there an easier solution already in Linq? This method will be used intense in the application.
Have you got tips to take with me, back to the drawing board?
You could just find the first value and check if ANY others are different, this will avoid having to eval the whole collection (unless the single different value is the last one)
public static bool IsQuantized(this MeasurementCollection items)
{
if(!items.Any())
return false; //or true depending on your use case
//might want to check that Template is not null, a bit a violation of level of demeter, but just an example
var firstProp = items.First().Template.Frequency;
return !items.Any(x=> x.Template.Frequency != firstProp);
}
Edit: to address Timwi's concerns about 3 enumerators:
bool same = <your default> ;
var first = items.FirstOrDefault();
if (first != null) // assuming it's a class
{
same = items.Skip(1).All(i => i.Template.Frequency == first.Template.Frequency);
}
Which still uses 2 enumerators. Not an issue for the average List<>
, but for a DB query it might pay to use the less readable:
bool same = <your default> ;
Item first = null;
foreach(var item in items)
{
if (first == null)
{
first = item;
same = true;
}
else
{
if (item.Template.Frequency != first.Template.Frequency)
{
same = false;
break;
}
}
}
First of a general linq advise. If you just what to know if there's exactly one in a collection use Single() or SingleOrDefault(). Count will potentially iterate the entire collection which is more than you need since you can bail out if there's two.
public static bool IsQuantized(this MeasurementCollection items)
{
var first = items.FirstOrDefault();
return first != null && items.Skip(1).All(i => first.Template.Frequency == i.Template.Frequency));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With