Check if all List Items have the same member value in C#

后端 未结 6 1529
遇见更好的自我
遇见更好的自我 2020-12-19 10:32

I\'m searching for a simple and fast way to check if all my Listitems have the same value for a member.

foreach (Item item in MyList)
{
    Item.MyMember = &         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 11:18

    I think this is the shortest and most elegant solution:

    bool allSame = list.GroupBy(item => item.TheProperty).Count == 1;
    

提交回复
热议问题