score:2
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;
}
score:1
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.
Source: stackoverflow.com
Related Articles
- LINQ query not returning desired result as string. Why?
- Returning LINQ to Entities Query Result as JSON string
- Returning a single property from a LINQ query result
- LINQ Query Returning Multiple Copies Of First Result
- My LINQ Query is Returning only 1 result
- Using LINQ query result for data source for GridControl c#
- Concat linq query result to string ?
- LINQ query not returning the correct result
- Returning the result from a linq query but with one value modified
- LINQ query result with string manipulation and regex
- LINQ Query returning IQueryable, need it to be a single result
- Can't get result of desired query in linq
- Linq query where there's a certain desired relationship between items in the result
- LINQ returning Null while same query when write in SQL return result
- Caching Method For Result String or Linq Query in Web Services C#
- Using LINQ query single string result in routing
- Can I assign the result of a Linq query to the source variable of the same query?
- Optional filters on LINQ Query returning unexpected result
- Get a string out of Linq query result
- How to get a correct result of binary string intersection in Entity to Linq query
- IQueryable returning empty result after performing LINQ query
- Linq dynamic order query result by typed input string
- Returning a Query Result to an ListView when joining tables Using Visual Basic and Linq
- Linq query with negated condition in Any not returning the expected result
- Convert Linq Query Result to Dictionary
- Filling a DataSet or a DataTable from a LINQ query result set
- How to OrderBy an integer in a string field in a Linq query
- How to convert LINQ query result to List?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Convert linq query to string array - C#
- How to make a LINQ query for this?
- Filter the "Includes" table on Entity Framework query
- Using linq to find any of multiple values of List<List<string>>
- Linq query with where against a nullable field
- Do conditional Where filters use deferred execution?
- List to datatable records
- IndexOutOfRangeException was unhandled by user code
- c# linq to sql with a stored procedure containing dynamic sql
- IQueryable LINQ cast error for group by using dynamic expression
- Sort List<string> like Excel columns sort
- LINQ to Entities - parse / filter object collection
- how to get two table combine record using entity framework with linq c#?
- Create a MethodCallExpression for a sub property
- Assert all in A belongs to B and all in B belongs to A
- Most efficient algorithm for merging sorted IEnumerable<T>
- Use Linq Where on child entity in lambda expression?
- Linq - Group then compare elements within each group
- How to add an AND/OR expression to the following dynamic linq expression
- why datetime.now not work when I didn't use tolist?
- .Net Enumerable extension that executes an Action and returns the same enumeration