[英]Why does list.Contains(x) not find x when list contains x,y,z
ACCEPTED THE FIRST COMPLETE ANSWER GIVEN. 接受第一个完整的答案。
THANKS TO ALL WHO RESPONDED! 感谢所有响应的人!
I'm new to C# and Visual Studio so forgive me if this is an elementary question. 我是C#和Visual Studio的新手,所以如果这是一个基本问题,请原谅我。
if (!exceptions.Contains(accountNodeID))
{
dummyTotals.Add(accountNodeID, accountNodeTotal);
}
List<string>
List<string>
string
string
Dictionary<string, int>
Dictionary<string, int>
If exceptions consists of only one element, Contains DOES recognize when accountNodeID is in exceptions . 如果异常仅包含一个元素,则在accountNodeID处于异常中时, 包含 DOES会识别。
If exceptions consists of 2+ elements, Contains DOES NOT recognize when accountNodeID is in exceptions . 如果异常由2个以上的元素组成,则accountNodeID处于exceptions中时, 无法识别包含 。
Screenshots of Visual Studio debugger [which SO won't allow me to post here because I had to create a new account :( ]: Visual Studio调试器的屏幕快照[不允许我在这里发布,因为我必须创建一个新帐户:(]:
Can anyone tell me the following? 谁能告诉我以下内容?
Very happy to provide additional relevant info, as needed! 非常高兴根据需要提供其他相关信息! Thanks very much!!!
非常感谢!!!
EDIT 编辑
Thank you for the info that has been supplied already. 感谢您提供的信息。 I'm looking over it now.
我正在看它。 For the record, the following is how the list is being created (iterating over nodes in an XML file and using Add ).
作为记录,以下是如何创建列表(在XML文件中的节点上迭代并使用Add )。
List<string> dummyList = new List<string>();
while (exceptionsIterator.MoveNext())
{
exceptions.Add(exceptionsIterator.Current.Value);
}
I thought this was adding list elements rather than concatenating values onto a string. 我认为这是在添加列表元素,而不是将值连接到字符串上。
EDIT 编辑
See my final comment on the answer for the ultimate resolution to this problem. 请参阅我对答案的最终评论,以最终解决该问题。
Your debugging screenshot shows that exceptions only contains one element: "541 139 434" 您的调试屏幕截图显示,异常仅包含一个元素:“ 541 139 434”
So the fault must lie in how exceptions is populated. 因此,故障必须在于异常的填充方式。
When you add to a List<string>
, you do so by calling .Add(string)
. 当您添加到
List<string>
,可以通过调用.Add(string)
。 I suspect there is code like: 我怀疑有这样的代码:
List<string> exceptions = new List<string>(String.Join(" ", accountNodeIds));
This is NOT the way to populate a list. 这不是填充列表的方法。 Instead:
代替:
List<string> exceptions = new List<string>(accountNodeIds);
or 要么
exceptions.AddRange(accountNodeIds);
or 要么
foreach (string accountNodeId in accountNodeIds) {
exceptions.Add(accountNodeId);
}
List.Contains
just loooks if the object inside equals another object, in case of string it looks if the whole string matches. List.Contains
只查找内部的对象是否等于另一个对象,如果是字符串,则查找整个字符串是否匹配。 You are looking for a substring since "541 139 434" is not the same as "139". 您正在寻找一个子字符串,因为“ 541 139 434”与“ 139”不同。
You can use String.Contains
with Enumerable.Any
: 您可以将
String.Contains
与Enumerable.Any
String.Contains
使用:
if (!exceptions.Any(e => e.Contains(accountNodeID)))
{
dummyTotals.Add(accountNodeID, accountNodeTotal);
}
You could also check if one of the "words" in the string (separated by white-space) is the one you're looking for, therefore use String.Split
: 您还可以检查字符串中的“单词”(用空格分隔)之一是否与您要查找的
String.Split
,因此请使用String.Split
:
if (!exceptions.Any(e => e.Split().Contains(accountNodeID)))
{
}
Because it looks for the exact match.Instead you should use Any
, in order to check whether any of your elements Contains your accountNodeId
: 因为它寻找精确匹配,所以应该使用
Any
来检查您的任何元素是否包含 accountNodeId
:
if (!exceptions.Any(x => x.Contains(accountNodeID))
Or don't do this.Store your Ids
seperately into your List
.Each item should contain one Id
.Then you can use Contains
and you will get the expected result. 或不执行此操作。将您的
Ids
单独存储到List
。每个项目应包含一个Id
。然后您可以使用Contains
来获得预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.