Link to home
Start Free TrialLog in
Avatar of MohitPandit
MohitPanditFlag for India

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
string[] selectedInput
.Where(p => selectedInput.Contains(p.Number.ToLower()))

Open in new window


Best Regards
Avatar of Arana (G.P.)
Arana (G.P.)

If I understand correctly you want to try something like this:
string[] selectedInput = { "one", "two", "Three"};
IEnumerable<string> result = from a in selectedInput
where a.Contains(p.Number.ToLower())
select a;
foreach (string item in result)
{
Console.WriteLine(item);
}

Open in new window

.
IndexOf is going to provide a like comparison; e.g. -
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();
        }
    }
}

Open in new window

Provides the following results -
User generated image
-saige-
Avatar of MohitPandit

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.
Do you want the entire row, the Number field or a subset of results?

-saige-
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.
select * from [MyTable]
where 
(
   [MyField] like '%Henri%'
   or
   [MyField] like '%Michael%'
   or
   [MyField] like '%Ram%'
)

Open in new window


Best Regards
Did you get chance to look into it?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I'll implement and let you know.
thanks