@BeUnique , based on my test, you could make some changes in your ContainsAny method to get what you want.
Here is a code example you could refer to.
static void Main(string[] args)
{
List<string> valcontains = new List<string>() { "MAP" };
List<string> Empcontains = new List<string>() { "M1" };
int EmpTagNo = 0;
DataTable tblData = new DataTable();
tblData.Columns.Add("EMPNO", typeof(string));
tblData.Columns.Add("EMPTAG", typeof(int));
tblData.Columns.Add("EMPDEP", typeof(string));
tblData.Rows.Add("KL+M1+XZ+X1+HM", "15", "CIV");
tblData.Rows.Add("KL+M1+M3+X1+HM", "20", "MEC");
tblData.Rows.Add("KL+M1+M5+X1+HM+M7", "40", "IT");
tblData.Rows.Add("KL+M1+M5+X1+HM+M7", "40", "IT");
tblData.Rows.Add("KL+M1+M5+X1+HM+M7", "40", "IT");
tblData.Rows.Add("KL+M1+M5+X1+HM+M7", "40", "IT");
tblData.Rows.Add("(MAP) KL+M1+M5+X1+HM+M7", "40", "CIV");
var Qry = (from r in tblData.AsEnumerable()
where
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), Empcontains)
select new
{
}).Count();
if (Qry > 0)
{
var Qry1 = (from r in tblData.AsEnumerable()
where
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), Empcontains)
select new
{
EMPTAG = r.Field<int>("EMPTAG"),
}).Distinct();
foreach (var n in Qry1)
{
EmpTagNo = n.EMPTAG;
}
Console.WriteLine(EmpTagNo);
}
}
public static class ContainsStringExtensions
{
public static bool ContainsAny(this string haystack, IEnumerable<string> filterList)
{
string[] arr = haystack.Replace(filterList.FirstOrDefault(), "").Split('+');
arr = arr.Where(x => !string.IsNullOrEmpty(x)).ToArray();
foreach (var item in arr)
{
if (!string.IsNullOrEmpty(item))
{
if (item.StartsWith(filterList.FirstOrDefault().Substring(0, 1)))
{
return false;
}
}
}
return haystack != null && filterList.Any(haystack.Contains);
}
}
Note: I write the code to check if the string contains the string starts with 'M' by split the initial string to the string array.
Result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.