Click here to Skip to main content
14,980,146 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please see this example
SQL
SELECT *
FROM Item WHERE (PartNumber LIKE 'KEA 12201 N%')

Result = 0 rows
SQL
SELECT *
FROM Item WHERE (PartNumber LIKE 'KEA%12201%N%')

Result = matching rows

Here space will replace with any character .then it will show the result

same thing I need to do in linq with lamda expression

ex:- List.where(u => u.PartNumber.StartsWith(partNumber))

but this will show no result while space contain search word

Please help me.

What I have tried:

List.where(u => u.PartNumber.StartsWith(partNumber))
Posted
Updated 22-Jun-21 6:12am
v2

This is taken fromLINQ to SQL Like (Contains, Startwith, Endswith) - Tutlane[^]
Operator	  Description
Contains()	  It is equivalent to '%string%'
StartsWith()  It is equivalent to 'string%'
EndsWith()    It is equivalent to '%string'


SELECT * FROM Item WHERE (PartNumber LIKE 'KEA%12201%N%')


So your LINQ query must look like this:

C#
List.Where(u => u.PartNumber.StartsWith("KEA")
&& u.PartNumber.Contains("12201")
&& u.PartNumber.Contains("N"))
   
Comments
Maciej Los 22-Jun-21 8:11am
   
5ed!
TheRealSteveJudge 22-Jun-21 8:11am
   
Thank you!
Try this:

C#
var result = List.Where(u => u.PartNumber.StartsWith("KEA") && u.PartNumber.Contains("12201") &&  u.PartNumber.Contains("N"))


Another way is to use Regex (non EF solution):
C#
Regex r = new Regex(@"KEA\s12201\sN")
var result1 = List.Where(u => r.IsMatch(u.PartNumber))


[EDIT]
Assuming that you want to split string_to_find into parts... (non EF solution)
C#
string string_to_find = "KEA 12201 N";
string[] parts = string_to_find.Split(' ');
var result = List.Where(u => parts.Any(p=> u.PartNumber.Contains(p))); //check if any part of string meets criteria
var result1 = List.Where(u => parts.All(p=> u.PartNumber.Contains(p))) //check if every part of string meets criteria


For further details, please see:
Enumerable.Any Method (System.Linq) | Microsoft Docs[^]
Enumerable.All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Method (System.Linq) | Microsoft Docs[^]

Good luck!
   
v3
Comments
TheRealSteveJudge 22-Jun-21 8:15am
   
The RegEx is even better as the positions of "12201" and "N" are exactly defined. 5*
Maciej Los 22-Jun-21 8:18am
   
Thank you.
Richard Deeming 22-Jun-21 8:17am
   
The regex option will only work if the list is already in memory. It won't be translated to a SQL query. :)
Maciej Los 22-Jun-21 8:18am
   
Agree.
saifulhaque 22-Jun-21 15:18pm
   
This string is not fixed.It is dynamic .I posted here an example.If the person search keyword in text box with space normally sql query will convert % and show the matching result.
We can use like this an example

Copy Code
SELECT *
FROM Item WHERE (PartNumber LIKE 'KEA%12201%N%')

May be the person search more spaces it should be show the matching result.

Example

Search key word is 'asd hjk tyu io 456 tyu' in sql direct query in management studio editor replacing space with % will show exact result.
Maciej Los 22-Jun-21 16:27pm
   
Seems, you want to split string_to_find into parts based on spaces. Wait, i'll update my solution.
[EDIT]
Done!
saifulhaque 23-Jun-21 5:35am
   
Thank you for your efforts !! . The code is working fine .
Maciej Los 23-Jun-21 5:51am
   
You're very welcome.
You've tagged this question as Entity Framework. If you're executing a query against the database, you'll need to use the appropriate function.

For EF 6.2 or later, use DbFunctions.Like:
C#
var result = Set.Where(u => DbFunctions.Like(u.PartNumber, "KEA%12201%N%"));

For EF Core 2.0 or later, use EF.Functions.Like:
C#
var result = Set.Where(u => EF.Functions.Like(u.PartNumber, "KEA%12201%N%"));
.net - Like Operator in Entity Framework? - Stack Overflow[^]
   
Comments
TheRealSteveJudge 22-Jun-21 8:18am
   
An elegant approach! 5*
Maciej Los 22-Jun-21 8:18am
   
5ed!
saifulhaque 22-Jun-21 15:13pm
   
This is not working .May be EF version different
Richard Deeming 23-Jun-21 3:09am
   
Well, since you haven't told us which version you're using, nor explained what "not working" means, we can't help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900