score:68

Accepted answer

I believe this would work:

public bool Validate(Crate crate)
{
    return crate.Sections
                .Select(x => x.Value.PixelsWide)
                .Distinct()
                .Count() < 2;
}

This will return true if crate.Sections is empty as well as when the elements are all the same (which is the behavior of your current function).

score:0

My version:

public bool Validate(Crate crate)
{
    return !crate.Sections
           .Any(a => crate.Sections
                     .Where(b => b.Value.PixelsWide != a.Value.PixelsWide).Any()
           );
}

score:0

This can be implemented as an extension method fairly trivially:

public static bool AllEqual<T1, T2>(this IEnumerable<T1> enumerable, Func<T1, T2> selector)
{
    using (var enumerator = enumerable.GetEnumerator())
    {
        if (!enumerator.MoveNext())
            return false;

        var first = selector(enumerator.Current);
        while (enumerator.MoveNext())
        {
            var current = selector(enumerator.Current);
            if (current == null)
            {
                if (first == null)
                    continue;

                return false;
            }

            if (current.Equals(first) == false)
                return false;
        }

        return true;
    }
}

score:0

My extension method for this looks like this:

public static bool AllAre<T>(this IList<T> self, Func<T, T, bool> predicate)
{
    if (self.Count == 0)
    {
        return true;
    }
    var first = self.First();
    return self.All(arg => predicate(arg, first));
}

And it is used like this:

myList.AllAre((x, first) => x.Value == first.Value);

score:1

I'm with @Stecya:

public class Crate
{
    IList<KeyValuePair<string,SectionConfiguration>> Sections ;

    public bool IsValid()
    {
        return Sections.All( x => x.Value.PixelsWide == Sections.FirstOrDefault().Value.PixelsWide ) ;
    }

    public class SectionConfiguration
    {
        public int PixelsWide ;
    }

}

score:2

Is grouping too slow?

public bool Validate(Crate crate)
{
    return crate.GroupBy(c => c.Value.PixelsWide).Count() < 2;
}

score:4

If you don't mind iterating through entire collection:

bool hasOneValue = crate.Sections.Select(s => s.Value.PixelsWide).Distinct().Count() == 1;

Or making it consistent with your code:

bool validateResult = crate.Sections.Select(s => s.Value.PixelsWide).Distinct().Count() <= 1;

score:7

Here's a variation on Stecya's answer that doesn't throw an exception for an empty collection.

var first = crate.Sections.Values.FirstOrDefault();
bool result = crate.Sections.Values.All(x => x.PixelsWide == first.PixelsWide);

score:16

Try this

var pixelsWide = rate.Sections.Values.First().PixelsWide;
bool result = crate.Sections.Values.All(x => x.PixelsWide == pixelsWide);

Related Query