如果列表中的所有项都具有相同的值,则需要使用该值,否则需要使用“otherValue”。我无法想到一种简单清晰的方法来实现这一点。当列表为空时,应返回“other”值。
另请参见优雅的编写循环以处理集合中的第一个项目的特殊逻辑。
var val = yyy.First().Value;
return yyy.All(x=>x.Value == val) ? val : otherValue;
我能想到的最简洁的方式。您可以通过将val内联来使其成为单行,但是First()将被评估n次,这将使执行时间翻倍。
要纳入评论中指定的“空集”行为,只需在上述两行之前添加一行:
if(yyy == null || !yyy.Any()) return otherValue;
.Any
是否可以在存在不同值的情况下使枚举提前退出? - Jeff Ogatax
的值不等于给定值 val
时,All
方法会立即终止。同理,当序列中存在一个元素 x
,其值不等于给定值 val
时,Any(x => x.Value != val)
方法也会立即终止。换句话说,All
和 Any
方法都表现出类似于逻辑运算符中的“短路”行为,类似于 &&
和 ||
运算符(这实际上就是 All
和 Any
方法所代表的)。 - jasonreturn yyy.Skip(1).All(x=>x.Value == val) ? val : otherValue;
- CaltorSkip
很可能会分配内存,因此不进行微观优化。 - Stefan Glienke一个适用于判断多个值是否相等的简单而快速的测试:
collection.Distinct().Count() == 1
collection.Distinct().Count() <= 1
。 - 3dGrabberDistinct
不会遍历整个集合,而Count
将通过Distinct
的迭代器进行一次遍历。 - NetMageDistinct
中使用相等比较器来处理特定类。或者,按照我选择使用此答案的方式,可以使用Select
首先映射到目标属性,例如:collection.Select(x => x.Property).Distinct().Count() == 1
。 - PJRobot虽然你肯定可以使用现有的序列操作符构建这样的设备,但在这种情况下,我倾向于将其编写为自定义序列操作符。类似于:
// Returns "other" if the list is empty.
// Returns "other" if the list is non-empty and there are two different elements.
// Returns the element of the list if it is non-empty and all elements are the same.
public static int Unanimous(this IEnumerable<int> sequence, int other)
{
int? first = null;
foreach(var item in sequence)
{
if (first == null)
first = item;
else if (first.Value != item)
return other;
}
return first ?? other;
}
这非常清晰、简洁,涵盖了所有情况,并且不会不必要地创建序列的额外迭代。
将其转换为适用于 IEnumerable<T>
的通用方法留作练习。:-)
T Unanimous<U, T>(this IEnumerable<U> sequence, T other)
或类似签名,这会使它变得有点复杂。 - Anthony Pegramreturn collection.All(i => i == collection.First()))
? collection.First() : otherValue;.
如果你担心为每个元素执行First()(这可能是一个有效的性能问题):
var first = collection.First();
return collection.All(i => i == first) ? first : otherValue;
public static partial class Extensions
{
public static Nullable<T> Unanimous<T>(this IEnumerable<Nullable<T>> sequence, Nullable<T> other, IEqualityComparer comparer = null) where T : struct, IComparable
{
object first = null;
foreach(var item in sequence)
{
if (first == null)
first = item;
else if (comparer != null && !comparer.Equals(first, item))
return other;
else if (!first.Equals(item))
return other;
}
return (Nullable<T>)first ?? other;
}
public static T Unanimous<T>(this IEnumerable<T> sequence, T other, IEqualityComparer comparer = null) where T : class, IComparable
{
object first = null;
foreach(var item in sequence)
{
if (first == null)
first = item;
else if (comparer != null && !comparer.Equals(first, item))
return other;
else if (!first.Equals(item))
return other;
}
return (T)first ?? other;
}
}
public int GetResult(List<int> list){
int first = list.First();
return list.All(x => x == first) ? first : SOME_OTHER_VALUE;
}
使用LINQ的替代方法:
var set = new HashSet<int>(values);
return (1 == set.Count) ? values.First() : otherValue;
我发现对于长度不超过6,000个整数的列表,使用HashSet<T>
比以下方法更快:
var value1 = items.First();
return values.All(v => v == value1) ? value1: otherValue;
HashSet<T>
最初比使用LINQ语句更快。然而,如果我在循环中执行此操作,则LINQ更快。 - Ɖiamond ǤeezeƦGetHashCode()
方法,而这很难正确地做到。请参考:https://dev59.com/yXRC5IYBdhLWcg3wOeSB#371348 获取更多细节。 - Cameron对上述简化方法的轻微变化。
var result = yyy.Distinct().Count() == yyy.Count();
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";
}