Enumerable.Except问题 [英] Enumerable.Except Problem

查看:136
本文介绍了Enumerable.Except问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释为什么像我想应该这是行不通的。

 双击[] = numbers1 {2.0, 2.1,2.2,2.3,2.3,2.3,2.4,2.5}; 
双[] = numbers2 2.2 {};

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

的foreach(在onlyInFirstSet双号)
Console.WriteLine(号码);

/ *
该代码将产生以下的输出:

2
2.1
2.3
2.4
2.5
* /

我希望是什么2,2.1,2.3,2.3,2.3, 2.4,2.5。为什么除返回不同的名单? ?这是一个错误。



更新:



好吧,完全错过了文档了这一点。滑稽的4人同样的答案回应。你可能会认为你只是最多投票谁最先回答了这个家伙。)


解决方案

为什么将除返回不同的名单?这是一个错误?




不。 除了产生的设置差异。请参阅文档




通过使用默认的相等比较器对值进行比较生成两个序列的差集。




要尽你想,只要使用其中,来适当地筛选值。例如:

 abcdddefffg。凡(!E =>中CDE。载有(e)条); //生产abfffg。 


Can someone explain why this doesn't work like I think it should.

double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.3, 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
*/

What I would expect is 2, 2.1, 2.3, 2.3, 2.3, 2.4, 2.5. Why would except return a distinct list? Is this a bug?

Update:

Ok, totally missed that point in the docs. Funny 4 people respond with the same answer. You would think you would just up vote the guy who answered it first.:)

解决方案

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

Nope. Except produces a set difference. See the documentation:

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

To do what you want, just use Where to filter the values appropriately. For example:

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

这篇关于Enumerable.Except问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆