'Checking if a property in a list of objects is equal for all items

This question is quite similar to this C# question, but rather than them all being equal to a particular known value, I want to know if they are all the same as each other no matter what that value is.

My list contains numerous objects such as:

public MyClass {
    public int Id { get; set; }
    public string MyProperty { get; set;}
}

My initial approach was:

List<MyClass> MyList = new List<MyClass>();
//... Add some values

string firstResult = MyList.First().MyProperty;
return MyList.All(x => x.MyProperty.Equals(firstResult))

But I feel there is a more elegant way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source