I have a SQL
query that retrieves only the names not containing any number:
...
WHERE Name NOT LIKE '%[0-9]%'
On the other hand, when trying to use this query in Lambda Expression
with different combinations as shown below, none of them is working does not work:
.Where(m => !m.EmployeeName.Contains("%[0-9]%")
or
.Where(m => !m.EmployeeName.Contains(".*[0-9].*")
How can I use NOT LIKE
method in Lambda Expression
?
Update: My lambda expression is shown below:
return Json(db.TEmployees
.Where(m => m.Status == Enums.Status.Active)
.AsEnumerable()
.Where(m => !Regex.IsMatch(m.EmployeeName, ".*[0-9].*"))
.Select(m => new { ID = m.EmployeeID, EmployeeName = m.EmployeeName }),
JsonRequestBehavior.AllowGet);
To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.
Lambda expressions can be parameterless or have one or more parameters. The following code snippet illustrates a lambda expression that doesn't have any parameters. () => Console. WriteLine("This is a lambda expression without any parameter");
The formula for calculating lambda is: Lambda = (E1 – E2) / E1. Lambda may range in value from 0.0 to 1.0. Zero indicates that there is nothing to be gained by using the independent variable to predict the dependent variable. In other words, the independent variable does not, in any way, predict the dependent variable.
Java String contains() method It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement. The contains() method in Java returns true only if this string contains “s” else false.
As far I know you can't apply regular expression in Linq to Entities. What I recommend to do is if you have other conditions call Where
method using them first, and then call AsEnumerable
to work with Linq to Object which allows you use regular expressions, so you can apply the condition you need:
var query= context.YourDbSet.Where(...)
.AsEnumerable()
.Where(m => !Regex.IsMatch(m.EmployeeName, @"\d"));
Or you can also do the following:
var query= context.YourDbSet.Where(...)
.AsEnumerable()
.Where(e=>e.!EmployeeName.Any(char.IsDigit));
A third solution could be using DbSet.SqlQuery method to execute your raw SQL query:
var query= context.YourDbSet.SqlQuery("SELECT * FROM Table WHERE Name NOT LIKE '%[0-9]%'");
Translating that to your scenario would be:
// This column names must match with
// the property names in your entity, otherwise use *
return Json(db.TEmployees.SqlQuery("SELECT EmployeeID,EmployeeName
FROM Employees
WHERE Status=1 AND Name NOT LIKE '%[0-9]%'"),
JsonRequestBehavior.AllowGet);// Change the value in the first condition for the real int value that represents active employees
You can use Regex.IsMatch
.
yourEnumerable.Where(m => !Regex.IsMatch(m.EmployeeName, @"\d"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With