0

I have a list of usr -

public static List<usr> usersList = new List<usr>();

where usr definition is -

public class usr
        {
            public string name;
            public string question;
            public string connID;
        }

When I Find/Exist for an existing usr I always get false (not exist), the code I am using to Find/Exist follows -

bool b = usersList.Exists(x => x.name == name);
usr student = usersList.Find(x => x.name == name);

when inspecting usersList just before the Find/Exist I can see that usr exist in the list.

What am I doing wrong ???

Shai
  • 355
  • 2
  • 5
  • 18
  • 1
    Can you show us a sample list which demonstrates this behaviour? – Tim Schmelter Aug 12 '14 at 09:53
  • try ... `x.name.Equals(name)` – S.L. Aug 12 '14 at 09:53
  • 1
    @S.L. That should make no difference. If anything, it may throw `NullReferenceException` – Sriram Sakthivel Aug 12 '14 at 09:53
  • 3
    Strings are case sensitive. Are you sure that the cases of all letters are equal? – Flat Eric Aug 12 '14 at 09:54
  • @S.L.: the `==` operator is overloaded in .NET so that it compares values not only references. – Tim Schmelter Aug 12 '14 at 09:54
  • @TimSchmelter yes you are right :) Then he should use `x.name.ToLower() == name.ToLower()` – S.L. Aug 12 '14 at 09:55
  • 2
    @S.L.: no, don't use `ToUpper` or `ToLower` to compare case insensitive, instead use the appropriate `StringComparison` in `String.Equals`. For example: `string.Equals(x.name,name,StringComparison.CurrentCultureIgnoreCase)`. Why? http://stackoverflow.com/questions/234591/upper-vs-lower-case – Tim Schmelter Aug 12 '14 at 09:56
  • @TimSchmelter ... i should shutdown my brain for today :P you are right +1 – S.L. Aug 12 '14 at 09:58
  • @TimSchmelter, the code shows in the original post. I added a usr (name="dd" question="how are you today" connID=hex value string) then I tried the Find(x => x.name == name)/Exist(x => x.name == name) functions and I always get false :/. @ S.L. I tried String.equals which also return false. Any other idea ? – Shai Aug 12 '14 at 11:06

1 Answers1

0

Reason for that was I had white space in the search string :(

I'd better go fishing today.

Thank you all for your comments.

Shai
  • 355
  • 2
  • 5
  • 18