MohitPandit
asked on
C#: Write LINQ with method approach to search like LIKE operator for each string array value
Hello Folks,
I've a string array and want to search in database table as per LIKE operator in LINQ with method approach not SQL approach in query.
Could you assist into it?
FYI, below code used IN approach. But I want to use selectedInput array; search like LIKE operator for each value
Best Regards
I've a string array and want to search in database table as per LIKE operator in LINQ with method approach not SQL approach in query.
Could you assist into it?
FYI, below code used IN approach. But I want to use selectedInput array; search like LIKE operator for each value
string[] selectedInput
.Where(p => selectedInput.Contains(p.Number.ToLower()))
Best Regards
IndexOf is going to provide a like comparison; e.g. -
-saige-
using System;
using System.Linq;
namespace EE_Q29181714
{
class Program
{
static void Main(string[] args)
{
var inputs = new[] { "OneDayAtATime", "twoCarsInDriveWay", "threEDogNight" };
var selects = new[] { "three", "TWO", "one" };
foreach(var select in selects)
{
var matches = inputs.Where(i => i.IndexOf(select, 0, StringComparison.OrdinalIgnoreCase) > -1);
foreach (var match in matches)
{
Console.WriteLine($"Match Found - Selector: {select}; Match: {match}");
}
}
Console.ReadLine();
}
}
}
Provides the following results --saige-
ASKER
Thanks for input.
Could you provide sample code using table with LINQ' method approach as per provided sample code in question?
Assume p.Number is table field.
Could you provide sample code using table with LINQ' method approach as per provided sample code in question?
Assume p.Number is table field.
Do you want the entire row, the Number field or a subset of results?
-saige-
-saige-
ASKER
Hi Saige,
I've a table and want to search on a field string array values in LINQ with method approach.
For example, I can write in T-SQL with below code. Which I want to convert in LINQ with method approach.
Best Regards
I've a table and want to search on a field string array values in LINQ with method approach.
For example, I can write in T-SQL with below code. Which I want to convert in LINQ with method approach.
select * from [MyTable]
where
(
[MyField] like '%Henri%'
or
[MyField] like '%Michael%'
or
[MyField] like '%Ram%'
)
Best Regards
ASKER
Did you get chance to look into it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I'll implement and let you know.
ASKER
thanks
Open in new window
.