How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?

后端 未结 9 599
孤城傲影
孤城傲影 2020-12-02 10:41

If all the items in a list have the same value, then I need to use that value, otherwise I need to use an “otherValue”. I can’t think of a simple and clear way of doing this

9条回答
  •  广开言路
    2020-12-02 11:30

    If a array is of type multidimension like below then we have to write below linq to check the data.

    example: here elements are 0 and i am checking all values are 0 or not.
    ip1=
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0

        var value=ip1[0][0];  //got the first index value
        var equalValue = ip1.Any(x=>x.Any(xy=>xy.Equals()));  //check with all elements value 
        if(equalValue)//returns true or false  
        {  
        return "Same Numbers";  
        }else{  
        return "Different Numbers";   
        }
    

提交回复
热议问题