score:2
public virtual User GetUser(string username)
{
return _db.Users.Single(x => x.UserName.ToLower() == username.ToLower());
}
But I don't think the compare will be much slower though....
score:10
String.Compare is not in the list of functions supported by entity framework (see Supported Function List) This means that when you perform this query entity framework will retrieve the whole data set required to execute this compare and perform the compare locally. This will be very slow.
A much better solution is to use == to compare strings, for example:
return _db.Users.Single(x => x.UserName == username);
score:4
I would use String.Equals
return _db.Users.Single(x =>
String.Equals(x.UserName, username, StringComparer.OrdinalIgnoreCase))
If i want to match "MartÃn" with "martin" (accent) as same, I would use String.Compare
.
return _db.Users.Single(x =>
string.Compare(x.UserName, username, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);
Source: stackoverflow.com
Related Articles
- Linq is this string.Compare in the query inefficient and is there a better way?
- Is there a better way of writing this Linq Query
- is there a better way to write this frankenstein LINQ query that searches for values in a child table and orders them by relevance?
- How to code this LINQ query in a better way
- Is there a better way to code this LINQ fragment?
- Is there a better way to achieve this with Linq or a better code
- Is there a better way to do this Linq query and function?
- Is there a better way to split this string using LINQ?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq Query to Compare with List of string values
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- c# linq generated query length over the limit. Is there any way we could up lift this limit?
- How to write this LINQ Query in a better way
- Is there a better LINQ Query to do this?
- Is there a better way of shortening this LINQ statement?
- Can i do this with dynamic LInq or is there a better way - Linq, C#, VS2008
- how to compare string linq with sub query with another linq with sub query
- My code is very inefficient for this simple Linq usage
- Is there a better way to do this LINQ statement block?
- Reduce the line of code for this LINQ query
- how could i handle this linq query and compare it with and int in an if-statement?
- Making this LINQ projection query better
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Is there a better way to code this Duplicate ID Checker?
- What is the performance optimum (or even better coding practise) for writing this Linq query
- Is there a better way to write this LINQ query?
- Better way to do this LINQ query
- Compare input double with datatable string column values within a range using between query(SQL query to Linq on datatable c# )
- Parameterized Linq Query -Is there a better performance option?
- How to convert this query string into LINQ to SQL query?
- PredicateBuilder and n+1 query
- Get Current Identity through Linq
- How to get a specific dictionary key value from ListDictionary?
- LINQ - Comparing a List of strings against a colon separated field
- linq query to element w/o foreach
- IQueryable does not contain a definition for 'Select'
- Group 2 similar requests Entity Framework / Linq in 1
- Count() returns incorrect result on IEnumerable<T> filled using LINQ
- SQL Query to LINQ (use join with two keys)
- Collection/CollectionView based on Linq query in Silverlight
- Traversing in a Tree Like Stucture using Linq
- LINQ ICollection of a Model to a string with Name and Surname from within another model
- How to write this LINQ ( or SQL) query?
- Return parent record with only children that meet a certain criteria
- Struct implementing IEnumerable yields unexpected error with LINQ
- Try within Linq query
- Linq OrderBy - Get Name By ID
- MongoDB .NET driver and text search
- How to use generic interface as generic constraint?
- Tracing LINQ TO SQL generated queries in ASP.NET MVC