Accepted answer

Must be a casing issue if the data is there, you should always use ToUpper() as that is optimised for string equality checks.

Also you should always use FirstOrDefault as Single will throw an exception if there are more than one matches and First will throw an exception if there are no matches. Try:

EDIT: I've also added Trim on the inputted value to sanitise the spaces. I've also added a null check on the locID parameter as that would blow up if it was passed is as null. Lastly, I added a ?? (coalesce) on the return statement just incase it is returning null and you're performing other things on that string (such as Trim or ToLowerCase) as that would result in an exception.

public string LocName(string locID)
    if (locID == null) return string.Empty;

    var name = (from a in idc.Locations
                where a.ID.ToString().Trim().ToUpper() == locID.Trim().ToUpper()
                select a.Name).FirstOrDefault();

    return name ?? string.Empty;   


Well firstly Single() will throw an exception if there are more than one matches to the query. I advise using FirstOrDefault() if you re expecting more than one answer back.

Also I would try using Trim() and ToUpper() to get rid of any whitespaces/case issues which might be preventing the strings from matching.

Related Articles