Aniket Narvankar

Aniket Narvankar

  • 559
  • 2.1k
  • 605.5k

Like Query in Linq

Jan 4 2021 3:06 PM
class Program
{
public static bool Like(string toSearch,string toFind)
{
if (toSearch.Contains(toFind))
return true;
else
return false;
}
static void Main(string[] args)
{
List<string> str = new List<string>();
List<string> strNew = new List<string>();
str.Add("abdecacd");
str.Add("facdgh");
str.Add("iabcacdjk");
str.Add("lmn");
str.Add("opqe");
str.Add("acbd");
str.Add("efgh");
string strToSearch= "acd,abc,abcacd,al";
string[] desc = strToSearch.Split(',');
for(int i = 0; i < str.Count; i++)
{
for(int j = 0; j < desc.Length; j++)
{
if(Like(str[i].ToString(),desc[j].ToString()))
{
strNew.Add(str[i].ToString());
break;
}
}
}
if(strNew != null)
{
foreach(string strPrint in strNew)
{
Console.WriteLine(strPrint);
}
}
}
}
 
How to write linq query for above code,here strToSearch value will be dynamic,user will enter as many comma seperated values as he wants to search,I want to write a linq query which return values from list which contain the values entered by user.
In above example suppose user entered acd,abc,abcacd,al,then output will be abdecacd,facdgh,iabcacdjk as these values in list contains acd or abc
I want to write a Linq query because in my application linq is used. 

Answers (1)