Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a String value contains any number by using Lambda Expression

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);

like image 624
Jack Avatar asked Mar 23 '16 12:03

Jack


People also ask

How to check if a string contains any numbers?

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.

Can we write a Parameterless lambda expression?

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");

How do you find lambda value?

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.

How do I use contains in Java 8?

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.


2 Answers

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));

Update:

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
like image 144
octavioccl Avatar answered Nov 06 '22 03:11

octavioccl


You can use Regex.IsMatch.

yourEnumerable.Where(m => !Regex.IsMatch(m.EmployeeName, @"\d"));
like image 44
Bon Macalindong Avatar answered Nov 06 '22 04:11

Bon Macalindong