How to Check if String is in List

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
I am trying to check if my list contains a certain string and not only part of the string but the exact mach.

Live example:

My postLinks list has the following strings: "car", "house", "firecracker", "laptop", "mouse"

Using this c# Block Code I get unexpected results:

Код:
IList<string> postLinks = project.Lists["postLinks"];
IList<string> postLinks2 = new List<string> (new string[] { "car", "house", "firecracker", "laptop", "mouse" });

//var postLinks = project.Lists["postLinks"];
bool alreadyProcessed;
string postLink = "car99";

alreadyProcessed = postLinks.Contains(postLink); //this is true even if "car99" is not in my list but only "car" is
alreadyProcessed = postLinks2.Contains(postLink); //this is false because in my manually defined list car99 doesn't exist


string ads = "dasD";// this is just dummy code so I can stay within the debug mode, so ignore it
Now my questions are:
Why alreadyProcessed is true when checking car99 in postLinks?
Is the postLinks list the same as postLinks2 ?


I have also attached the test project below
 

Вложения

LaGir

Client
Регистрация
01.10.2015
Сообщения
211
Благодарностей
852
Баллы
93
Why alreadyProcessed is true when checking car99 in postLinks?
It looks like a bug. It turns out that in the implementation of IZennoList comparisons occur on the contrary. In "C#-List" - (element==postLink), in IZennoList - (element.Contains(postLink)). In any case, it turns out this way in my experiments)

If you need only read IZennoList list, you can immediately convert it to the "C#-list".
C#:
IList<string> postLinks = project.Lists["postLinks"].ToList();
 
  • Спасибо
Реакции: zenfreak

Эрнесто Че Гевара

Пользователь
Регистрация
16.11.2017
Сообщения
50
Благодарностей
10
Баллы
8

Обращаем Ваше внимание на то, что данный пользователь заблокирован.
Не рекомендуем проводить с Эрнесто Че Гевара какие-либо сделки.

PHP:
var list = project.Lists["title"];
string id = project.Variables["title"].Value;
if(list.Contains(id))
{
    return "true";
}
else
{
    return "false";
}
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
It looks like a bug. It turns out that in the implementation of IZennoList comparisons occur on the contrary. In "C#-List" - (element==postLink), in IZennoList - (element.Contains(postLink)). In any case, it turns out this way in my experiments)

If you need only read IZennoList list, you can immediately convert it to the "C#-list".
Thank you for the solution. Yes, this time I only need this for reading but as you have noted, I might be a bug that needs to be fixed.


PHP:



    • var list = project.Lists["title"];
    • string id = project.Variables["title"].Value;
    • if(list.Contains(id))
    • {
    • return "true";
    • }
    • else
    • {
    • return "false";
    • }
Please be aware that your code does not return the expected result if your title list is really big - it might fool you in the beginning when the list is small.
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)