Reputation: 304
I am searching a table records which has a FirstName = "abcxyz"
successfully with below Linq
query.
db.People.Where(p => searchString.Contains(p.FirstName).ToList();
But I would like to search the table records which contains any of the letter in FirstName = "abcxyz"
Like we do in SQL -
Any suggestions would be helpful here.
Upvotes: 1
Views: 2905
Reputation: 1308
You can use below methods in LINQ, To get data using LIKE operator in SQL
Example :
1) if you want to get data starting with some letter we are using
In SQL :-
select * from People where firstname LIKE '%abc';
In LINQ : -
db.People.Where(p => p.firstname.StartsWith(abc));
2) if you want to get data contains any letter we are using
In SQL
select * from people where firstname LIKE '%abc%';
In LINQ
db.people.where(p => p.Contains(abc));
3) if you want to get data ending with some letter we are using
In SQL
select * from people where firstname LIKE '%abc';
In LINQ
db.people.where(p => p.firstname.EndsWith(abc));
Upvotes: 2
Reputation: 223247
Looking at the SQL , what you need is:
p.FirstName.Contains(searchString)
so your query would be:
db.People.Where(p => p.FirstName.Contains(searchString)).ToList();
Upvotes: 5