0

I have a variable which holds domain name and username such [DOMAIN]/[User]. When i use this variable in a linq query lookup, it does not find the user because it replaces a single backslash with a double backslash.

For example:

UPDATED

List<string> lstUsersToRemove = new List<string>() {"TEST\acuba","TEST\test2", "TEST\test3" };

foreach(string userName in lstUsersToRemove)
{
    var user = listUsers.SingleOrDefault(x => x.UserName == userName);
    if(user != null)
       listUsers.Remove(user);
}

user is always null because it looks to match with the userName that has double backslash. It looks for "TEST\acuba" or "TEST\test2". I tested it when running it by manually removing the second backslash and was able to find the correct user.

Is there a way to correct this or an alternative way of doing the lookup without it failing because of the backslash?

Thanks in advance for your help.

Alex Cuba
  • 13
  • 3
  • 4
    None is replacing a variable content with another content. It is the debugger that shows you the single backslash doubling it to differentiate between a real backslash and the first char of an escape sequence. Please show your code to better understand your problem – Steve Apr 12 '18 at 18:51
  • 3
    `string userName = [DOMAIN]\\[User];` This isn't valid C#. Can you show how you're really producing the `userName` value? – StriplingWarrior Apr 12 '18 at 18:55
  • 1
    something that may not relate to the question: if you use SingleOrDefault(), you will get exceptions if your listUsers contains more than 1 value that is returned. Use FirstOrDefault instead. – King of the North Apr 12 '18 at 18:59
  • Can't debug or troubleshoot pseudocode. – NetMage Apr 12 '18 at 19:13
  • Sorry didn't mean to end my comment there with that was pseudo code. Thanks for the responses guys. I appreciate any bit of help. I updated my code to make it more precise as to what I am trying to do. – Alex Cuba Apr 12 '18 at 19:21
  • Did you look at the result of your sample strings? The backslash is used for character escapes in C# and `\a` represents the alert (or BEL) ASCII character 0x07, `\t` represents the TAB 0x09 character. So your `lstUsersToRemove` has no backslashes in it. – NetMage Apr 12 '18 at 20:32
  • @NetMage that was it!! It was treating \a as the ACII character and therefore needed to be converted to a literal in order to display correctly. I removed the conversion to a literal and the comparison worked as expected. Thank you!! – Alex Cuba Apr 12 '18 at 20:46
  • C# has the literal string (`@"..."`) for disabling (most) escapes and there is also the escape for the escape meta character `\\`. – NetMage Apr 12 '18 at 21:18

2 Answers2

0

How about "normalizing" the comparisons by replacing the double backslashes with a single.

List<string> lstUsersToRemove = new List<string>() {"TEST/acuba","TEST/test2", "TEST/test3" };

foreach(string userName in lstUsersToRemove)
{
    var user = listUsers.SingleOrDefault(x => x.UserName.Replace("//", "/") == userName.Replace("//", "/"));
    if(user != null)
        listUsers.Remove(user);
}
  • thank you for the suggestion. However, it is not a casing issue because if I step through the code and simply remove the second backslash before entering the foreach loop from userName, the query returns the correct result. Much appreciated though. – Alex Cuba Apr 12 '18 at 19:22
  • I also tried normalizing the comparisons. i tried: var user = listUsers.FirstOrDefault(x => x.UserName.Replace(@"\\", @"\") == userName.Replace(@"\\", @"\")); – Alex Cuba Apr 12 '18 at 19:49
0

can you try using .Equals instead of ==... This has given me trouble in the past. Try this

List<string> lstUsersToRemove = new List<string>() {"TEST\acuba","TEST\test2", "TEST\test3" };

foreach(string userName in lstUsersToRemove)
{
    var user = listUsers.SingleOrDefault(x => x.UserName.Equals(userName));
    if(user != null)
       listUsers.Remove(user);
}

== Expects the objects to be the same, where .Equals actually checks the values.

phishfordead
  • 377
  • 2
  • 7