question

GaniTPT avatar image
0 Votes"
GaniTPT asked JackJJun-MSFT answered

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

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)


dotnet-csharp
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When you say 'How to avoid eliminate other values like contains M2,M3,M4,etc...' do you mean that there shouldn't be any items in filterList that don't begin with the same character as haystack?

0 Votes 0 ·

Thanks for your immediate reply..

Let us consider only M1 values an remaining should not consider.

For example, total list values {"M1","M2","M3","M4","M5","M6","M7","M8"}

if i am passing the values "M1". It should consider only this row.

If i have any "M1+M2+M5" then we should not consider this row values.

KL+M1+XZ+X1+HM ===> only Row should consider**. Because there is no other value like M2....M8
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)

below is my query

var Qry = (from r in tblEmployee.AsEnumerable()
where
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), OnlyNum) &&
ContainsStringExtensions.ContainsAny(r.Field<string>("EMPNO"), Empcontains)
select new
{
EmpNo = r.Field<string>("EmpRecNo"),
}).Distinct();

0 Votes 0 ·

Ah, do you mean where only one string from filterList can be found in haystack?

static void Main(string[] args) {
    Console.WriteLine("KL+M1+XZ+X1+HM".ContainsAny(new[] { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" }));
    Console.WriteLine("KL+M1+M3+X1+HM".ContainsAny(new[] { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" }));
    Console.WriteLine("KL+M1+M5+X1+HM+M7".ContainsAny(new[] { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" }));
}

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


This produces the output you're describing.

0 Votes 0 ·
Show more comments

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@GaniTPT, 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.






image.png (1.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.