简体   繁体   中英

LINQ query not returning results when WHERE clause has special character

In my script I am dynamically building a string and then passing the string into the WHERE clause of a LINQ TO ENTITIES statement. When the statement is run it fails to find any results and I can't figure out why. If I explicitly hard code a value into the variable that gets passed into the statement it works fine, but if I let the system build it for me it fails. Why is this failing?

for (int i = 1; i <= TotalUsers; i++)
{
    var CurrentUser = db.Users.Where(u => u.ID == i).Select(u => u.ADUserName).First();
    UserPrincipalData = UserPrincipal.FindByIdentity(Context, CurrentUser);
    var UserDirectoryData = UserPrincipalData.GetUnderlyingObject() as DirectoryEntry;

    var Manager = (string)UserDirectoryData.Properties["manager"].Value;

    if (String.IsNullOrEmpty(Manager))
    {
        Manager = @"Company\Matthew.Verstraete";
    }
    else
    {
        var StartIndex = Manager.IndexOf("=") + 1;
        var EndIndex = Manager.IndexOf(",", StartIndex);
        var Length = EndIndex - StartIndex;
        Manager = Manager.Substring(StartIndex, Length);
        Manager = @"Company\" + Manager.Replace(" ", ".");
    }

    var ManagerID = db.Users.Where(u => u.ADUserName == Manager).Select(u => u.ID).FirstOrDefault();
    var UpdatedUser = db.Users.Find(i);
    UpdatedUser.ManagerID = ManagerID;
    db.SaveChanges();
}

Working via DeBug if the IF statement is true and the variable is hardcoded to me then the ManagerID is set correctly from the query, if it fails and goes to the ELSE clause it will fail even if the Manager variable is dynamically set to me as well.

Edit: The code does not throw any errors in debug. The variable Manager always gets a the proper value in form of Company\\\\First.Last (C# seems to be escaping the backslash). So I can't figure out why the LINQ query fails when the name is set dynamically but not statically. I need to be able to dynamically set the Manager name and pass it to the query so I can associate an employee to there manager correctly.

It would help to wrap your code in a try catch block and to give us the exception that was being thrown.

But my guess is that you might be getting an ArgumentOutOfRangeException

If this is the issue, then for your StartIndex or your EndIndex you might be getting -1, which means that the string searched for does not exist.

This would make your Length variable a negative number, and when using substring on a negative value would lead to the exception.

I would step through and see the values of your start and end index.

IndexOf: https://msdn.microsoft.com/en-us/library/7cct0x33(v=vs.110).aspx

Substring: https://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx


Ok, I think I found the issue.

So you're using the @ sign in your else statement. However, you said that the value is Company\\\\First.Last. With the @ sign, the string gets considered as literal, so it's searching for "Company\\\\First.Last"

If you want to search for "Company\\First.Last" you should remove the @ sign from the else statement.

I read this post to help me understand the issue: What's the @ in front of a string in C#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM