高效的 Linq Enumerable 的 'Count() == 1'测试

发布于 2024-11-08 14:18:19 字数 298 浏览 0 评论 0原文

与此 问题 类似,但针对 Linq 进行了改写:

您可以使用 Enumerable;.Any() 测试可枚举是否包含数据。但是,测试枚举是否包含单个值(即 Enumerable.Count() == 1)或大于单个值(即 Enumerable)的有效方法是什么? .Count() > 1) 而不使用昂贵的计数操作?

Similar to this question but rephrased for Linq:

You can use Enumerable<T>.Any() to test if the enumerable contains data. But what's the efficient way to test if the enumerable contains a single value (i.e. Enumerable<T>.Count() == 1) or greater than a single value (i.e. Enumerable<T>.Count() > 1) without using an expensive count operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

简美 2024-11-15 14:18:19
int constrainedCount = yourSequence.Take(2).Count();

// if constrainedCount == 0 then the sequence is empty
// if constrainedCount == 1 then the sequence contains a single element
// if constrainedCount == 2 then the sequence has more than one element
int constrainedCount = yourSequence.Take(2).Count();

// if constrainedCount == 0 then the sequence is empty
// if constrainedCount == 1 then the sequence contains a single element
// if constrainedCount == 2 then the sequence has more than one element
花心好男孩 2024-11-15 14:18:19

一种方法是编写一个新的扩展方法

public static bool IsSingle<T>(this IEnumerable<T> enumerable) {
  using (var enumerator = enumerable.GetEnumerator()) {
    if (!enumerator.MoveNext()) {
      return false;
    }
    return !enumerator.MoveNext();
  }
}

One way is to write a new extension method

public static bool IsSingle<T>(this IEnumerable<T> enumerable) {
  using (var enumerator = enumerable.GetEnumerator()) {
    if (!enumerator.MoveNext()) {
      return false;
    }
    return !enumerator.MoveNext();
  }
}
佼人 2024-11-15 14:18:19

此代码采用了 LukeH 的出色答案,并将其包装为 IEnumerable 扩展,以便您的代码可以处理 NoneOne>许多而不是012

public enum Multiplicity
{
    None,
    One,
    Many,
}

在静态类中,例如 EnumerableExtensions

public static Multiplicity Multiplicity<TElement>(this IEnumerable<TElement> @this)
{
    switch (@this.Take(2).Count())
    {
        case 0: return General.Multiplicity.None;
        case 1: return General.Multiplicity.One;
        case 2: return General.Multiplicity.Many;
        default: throw new Exception("WTF‽");
    }
}

This code take's LukeH's excellent answer and wraps it up as an IEnumerable extension so that your code can deal in terms of None, One and Many rather than 0, 1 and 2.

public enum Multiplicity
{
    None,
    One,
    Many,
}

In a static class, e.g. EnumerableExtensions:

public static Multiplicity Multiplicity<TElement>(this IEnumerable<TElement> @this)
{
    switch (@this.Take(2).Count())
    {
        case 0: return General.Multiplicity.None;
        case 1: return General.Multiplicity.One;
        case 2: return General.Multiplicity.Many;
        default: throw new Exception("WTF‽");
    }
}
挽清梦 2024-11-15 14:18:19

另一种方式:

bool containsMoreThanOneElement = yourSequence.Skip(1).Any();

或者对于 1 个元素:

bool containsOneElement = yourSequence.Any() && !yourSequence.Skip(1).Any();

Another way:

bool containsMoreThanOneElement = yourSequence.Skip(1).Any();

Or for exactly 1 element:

bool containsOneElement = yourSequence.Any() && !yourSequence.Skip(1).Any();
美煞众生 2024-11-15 14:18:19

高效的 Count() == n 测试:

public static bool CountIsEqualTo<T>(this IEnumerable<T> enumerable, int c) 
{
    using (var enumerator = enumerable.GetEnumerator()) 
    {
        for(var i = 0; i < c ; i++)
            if (!enumerator.MoveNext()) 
                return false;
        
        return !enumerator.MoveNext();
    }
}

编辑,11 年后:
现在我可能会把它写成它

public static bool CountIsEqualTo<T>(this IEnumerable<T> enumerable, int c) 
{
    return enumerable.Take(c + 1).Count() == c;
}

是已接受答案的通用版本。

Efficient Count() == n test:

public static bool CountIsEqualTo<T>(this IEnumerable<T> enumerable, int c) 
{
    using (var enumerator = enumerable.GetEnumerator()) 
    {
        for(var i = 0; i < c ; i++)
            if (!enumerator.MoveNext()) 
                return false;
        
        return !enumerator.MoveNext();
    }
}

Edit, 11 years later:
Nowadays I'd probably write this as

public static bool CountIsEqualTo<T>(this IEnumerable<T> enumerable, int c) 
{
    return enumerable.Take(c + 1).Count() == c;
}

It's a generalized version of the accepted answer.

月牙弯弯 2024-11-15 14:18:19

对于对象的 linq,如果有多个元素,SingleOrDefault 就会抛出异常,因此如果您自己推出一个元素,情况可能会更好。

编辑:现在我已经看到了LukeH的答案,我不得不说我更喜欢它。真希望我自己也能想到这一点!

With linq to objects, SingleOrDefault throws if there is more than one element, so you're probably best off if you roll your own.

EDIT: Now I've seen LukeH's answer, and I have to say I prefer it. Wish I'd thought of it myself!

奢望 2024-11-15 14:18:19
bool hasTwo = yourSequence.ElementAtOrDefault(1) != default(T);

...如果类的值可以为空,这可能对我们有用。

bool hasTwo = yourSequence.ElementAtOrDefault(1) != default(T);

...in case of class where values can be null this could maybe we useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文