score:31

Accepted answer

you can check by using all method, presumably your list have students:

var firststudent = students.first();
students.all(s => s.age == firststudent.age);

score:0

if you can't use linq, you can always loop all the students:

private bool sameage (list<student> students)
{
    int auxage = students.count > 0? students[0].age : 0;

    foreach (student stu in students)
    {
        if (stu.age != auxage) 
            return false;
    }
    return true;
}

score:0

if you just have to check this once, the marked answere is best solution. to use it multiple times in your code just write a static extension to check equality of property:

public static bool getidenticproperty<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> predicate)
{
    ienumerable<tsource> enumerable = source as ilist<tsource> ?? source.tolist();
    if ((!enumerable.any()) || (enumerable.count() == 1))
        return true; // or false if you prefere
    var firstitem = enumerable.first();
    bool allsame = enumerable.all(i => equals(predicate(i), predicate(firstitem)));
    return allsame;
}

if you want to use the value of the property later on lets return that value:

public static tkey getidenticproperty<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> predicate)
{
    ienumerable<tsource> enumerable = source as ilist<tsource> ?? source.tolist();
    if (!enumerable.any())
        return default(tkey);
    var firstitem = enumerable.first();
    bool allsame = enumerable.all(i => equals(predicate(i), predicate(firstitem)));
    return allsame ? predicate(firstitem) : default(tkey);
}

but using this code you have to check weather the returnd value is null or default(tkey) relating to the type of property.

score:3

if students can have 0 elements you can do this:

var firststudent = students.firstordefault();
var aresame =students.all(s => s.age == firststudent.age);

score:4

just a random answer - not sure i'd do it this way in reality, but this will be brutally efficient:

  • will use typed iterators if duck-typing is available (unlike linq which won't) - and note that list<t> does offer duck-typed iterators
  • no double-iterations
  • no failures etc for empty sequences
  • no delegates, capture-contexts, etc
  • etc

code:

using(var iter = students.getenumerator()) // a list<t>.enumerator struct
{
    if(!iter.movenext()) return true; // or false, as you define for "empty"

    int age = iter.current.age;
    while(iter.movenext())
        if(iter.current.age != age)
            return false;
    return true;
}

score:11

if you want to do this in one query, not two (which is generally bad practice),

bool allagesarethesame = (students.select(s => s.age).distinct().count() < 2);

will do it for you.

this will also return true in the trivial case where you have no students at all, rather than throw an exception. (you could do == 1 rather than < 2 to return false in the trivial case instead.)


Related Query