How do i get given string only and exclude the others match row in containsany c#.net

BeUnique 2,332 Reputation points
2021-09-23T10:09:33.487+00:00

I am using ContainsAny to filter the data.

But, when i used this, it will consider all related string.

For example, my string value is Empcontains = "M1". It should exactly match only M1 and not others like M2,M3,M4,etc.

Empcontains = "M1"
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), Empcontains)

How to avoid eliminate other values like contains M2,M3,M4,etc...

below is the sample code for understandings.

Empcontains = "M1"
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), Empcontains)

public static class ContainsStringExtensions
{
    public static bool ContainsAny(this string haystack, IEnumerable<string> filterList)
    {
        //return filterList.Any(haystack.Contains);
        return haystack != null && filterList.Any(haystack.Contains);
    }
}

Example Data

KL+M1+XZ+X1+HM ===> only this should come**
KL+M1+M3+X1+HM ===> should not consider this data. because it contains (M1+M3)
KL+M1+M5+X1+HM+M7 ===> should not consider this data. because it contains (M1+M5+M7)

Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-09-24T06:10:15.96+00:00

    @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:

    134933-image.png


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.