score:5
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
score:1
Try this more information link :
return Json(db.TEmployees
.Where(m => m.Status == Enums.Status.Active && !m.EmployeeName.Any(char.IsDigit))
.Select(m => new { ID = m.EmployeeID, EmployeeName = m.EmployeeName }).ToList(),
JsonRequestBehavior.AllowGet);
EDIT: Add ToList() in return json
score:1
The only solution using Linq-To-Entities I can think of is to define a string array and see if your string contains any of them:
string[] digits = new { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
...Where(m => !digits.Any(m.EmployeeName.Contains(d));
I can imagine this will be a slow query though for big datasets, so I would just execute the sql through EF.
score:1
There is no general LINQ to Entities solution.
However, if you target only Sql Server database, you can use SqlFunctions.PatIndex canonical function like this:
db.TEmployees
.Where(m => m.Status == Enums.Status.Active &&
SqlFunctions.PatIndex("%[0-9]%", m.EmployeeName) == 0)
//...
score:2
You can use Regex.IsMatch
.
yourEnumerable.Where(m => !Regex.IsMatch(m.EmployeeName, @"\d"));
score:2
EF is limited in its capability to generate the exact SQL you want. I do not know of a specific expression that will generate the pattern [0-9]
in your LIKE
clause.
The list of String Functions that are supported by EF are documented on MSDN. None of which can be used to determine if a string contains an arbitrary digit or not.
Some other options are:
- Use the exact SQL you want in C# and call
ExecuteStoreCommand
- Return more objects than you need from the DB and filter in memory using
AsEnumerable()
The Equivalent SQL would be something like
SELECT *
FROM TEmployees
WHERE Status = {code for active status}
AND Name NOT LIKE '%[0-9]%'
Source: stackoverflow.com
Related Query
- Check if a String value contains any number by using Lambda Expression
- Check if string list contains any enum string value
- Append index number to duplicated string value in a list - by using Lambda
- How to check list A contains any value from list B?
- Check if a string has at least one number in it using LINQ
- How to use Linq to check if a list of strings contains any string in a list
- Check if a string contains particular characters in any order
- String Join Using a Lambda Expression
- Get Value and Count of that value using LINQ or lambda expression
- Access property in lambda expression from string when using LINQ
- Using a linq or lambda expression in C# return a collection plus a single value
- Filter IEnumerable<object> based on whether string property contains any string value of another List<string>
- How to convert data using lambda expression or using any other logic?
- Find an attribute that contains a ceratin string and modifying its Value in c# using LINQ
- What's the lambda expression to check to see if one property in any of a group of objects matches any of a group of strings?
- Using Lambda OrderBy Comparer to give weight to a specific string value
- c# Lambda Expression - Get property value from string
- Check if a CSV string contains a given value C#
- Using C# Lambda to split string and search value
- How to check if String contains any of the strings in List/Array
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Using LINQ to check if a string contains a list of strings or characters
- Check a List<List<int>[]> contains a value using Linq
- check if string contains dictionary Key -> remove key and add value
- Using StringBuilder.Append() in a lambda expression is producing an empty string
- How to write the same code using Lambda Expression
- Lambda expression to check if value is null or equals
- How to check if a string contains a particular string using linq?
- Assign Value in List using lambda expression
- Get records associated to null value if no match is found using lambda expression
More Query from same tag
- Linq query performance with new object in `Where`
- if statement in Linq query object creation
- Null value cannot be assigned - LINQ query question
- Joining 2 Tables using Linq to return as list
- SQL query LINQ To SQL equivalent
- System.Linq.Expressions - Operation is not valid due to the current state of the object
- LINQ using Contain with Include
- How to construct List of objects inside linq
- Linq statement for Orders reporting
- How to access properties of a linq query in aspx page?
- How to send LINQ query to a View without using ViewModel
- I can't Distinct, Sum, or Count from 3 tables with group clauses
- How to efficiently retrieve a list of random elements from an IQueryable?
- How to return value from Action()?
- leaderboard ranking using LINQ rank number and same point
- How to map LINQ EntitySet to List property in property setter?
- Linq Sort with Text value, not lambda
- Is it possible to convert a string to its ExpressionType counterpart?
- BETWEEN operator for string in LINQ to SQL query
- LINQ join, group by, sum
- Take the first five elements and the last five elements from an array by one query using LINQ
- How do you convert a List of object to a one dimensional array?
- Group data into a type and list of object members
- Rewrite of an Expression Tree
- How to return a child collection with joins linq query
- N-Enumerables In a LINQ Statement?
- String matching problem in Entity framework. Works for a string literal but not for a string variable
- Include nested of nested child with LINQ
- List Iteration to Create Custom List in C#(Linq)
- Dynamic OR in LINQ without the PredicateBuilder