加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Enumerable.Except问题

发布时间:2020-12-15 04:26:12 所属栏目:百科 来源:网络整理
导读:有人可以解释为什么这不像我认为的那样有效. double[] numbers1 = { 2.0,2.1,2.2,2.3,2.4,2.5 };double[] numbers2 = { 2.2 };IEnumerabledouble onlyInFirstSet = numbers1.Except(numbers2);foreach (double number in onlyInFirstSet) Console.WriteLine(
有人可以解释为什么这不像我认为的那样有效.
double[] numbers1 = { 2.0,2.1,2.2,2.3,2.4,2.5 };
double[] numbers2 = { 2.2 };

IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);

foreach (double number in onlyInFirstSet)
    Console.WriteLine(number);

/*
 This code produces the following output:

 2
 2.1
 2.3
 2.4
 2.5
*/

我期望的是2,2.5.为什么除了返回一个不同的列表?这是一个错误吗?

更新:

好的,完全错过了文档中的那一点.有趣的4个人回答相同的答案.你会认为你会先投票给那个先回答它的人.:)

解决方法

Why would except return a distinct list? Is this a bug?

不.除了产生一组差异.见documentation:

Produces the set difference of two sequences by using the default equality comparer to compare values.

要做你想做的事,只需使用Where来适当地过滤值.例如:

"abcdddefffg".Where(e => !"cde".Contains(e));       // Produces "abfffg".

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读