Possible Duplicate:
How to do SQL Like % in Linq?
Im using mvc 3 and entity framework. How do i do a like search with linq or a lambda expression. Please assisst
Possible Duplicate:
How to do SQL Like % in Linq?
Im using mvc 3 and entity framework. How do i do a like search with linq or a lambda expression. Please assisst
Since the goal is for EF expressions to be parsed into SQL, where the LIKE predicate should be applied, there are at least 3 ways to do this, depending on where you want the % wildcard to be placed
C#:
.Where(customer => customer.Name.StartsWith("Bloggs"))
=> SQL
WHERE c.Name LIKE 'Bloggs%'
C#:
.Where(customer => customer.Name.Contains("Bloggs"))
=> SQL
WHERE c.Name LIKE '%Bloggs%'
C#:
.Where(customer => customer.Name.EndsWith("Bloggs"))
=> SQL
WHERE c.Name LIKE '%Bloggs'
If applicable, StartsWith should be preferred over the other two, given that it has a better chance of using an index on the column. (%x% will generally result in an index or table scan).
it's duplicate to - LIKE operator in LINQ How to do SQL Like % in Linq?
Typically you use String.StartsWith/EndsWith/Contains. For example:
var Code = .Where(p => p.Code.Contains("BALTIMORE"))
var Code = .Where(p => p.Code.StartsWith("BALTIMORE"))
var Code = .Where(p => p.Code.EndsWith("BALTIMORE"))