locked
Compare list of string C# RRS feed

  • Question

  • User1655374113 posted

    Hello,

    I have two List<string>.

    List<string1> and List<string2>

    List<string2> is subset of List<string1>.

    I want to compare each string in List<string2> to List<string1> and if the string exists, I want to Check that string as true.

    How to do this ?

    TIA

    Friday, October 10, 2014 10:18 AM

Answers

  • User281315223 posted

    Below is my code which I written. I just want to know, is there any effective way to do this.

    foreach (var str in strings2)
    {
       for (int i = 0; i < strings1.Count() - 1; i++)
        {
          if (strings1[i].Name == str.Name)
          {
             strings1[i].IsChecked = true;
           }
        }
    }

    You could just loop through the values that match :

    // Loop through your collection of strings
    foreach(var s1 in strings1)
    {
        // Check the values that are present in the other collection
        s1.IsChecked = strings2.Any(s2 => s1.Name == s2.Name);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2014 3:57 PM

All replies

  • User-484054684 posted
    List<string> string1 = new List<string>();
          string1.Add("test1");
          string1.Add("test2");
          string1.Add("test3");
          string1.Add("test4");
    
    
          List<string> string2 = new List<string>();
          string1.Add("test2");
          string1.Add("test3");
    
          foreach (string str in string2)
          {
            if (string1.Contains(str) == true) //Checks if String1 list has the current string.
            {
              continue;
            } 
            else
            {
              //Write your logic
            }
          }

    Hope the above snippet helps to modify according to your logic

    Friday, October 10, 2014 10:30 AM
  • User-760709272 posted
    List<string> listA = new List<string> { "A", "B", "C", "D" };
    List<string> listB = new List<string> { "B", "C", "E" };
    
    // matching has the elements that are found in the parent list
    // B and C in this instance
    List<string> matching = listA.Intersect(listB).ToList();
    

    Friday, October 10, 2014 10:41 AM
  • User281315223 posted

    I agree with Aidy that the best approach would be to use the Enumerable.Intersect() method, which will provide you with a collection containing all of the values within the Intersection of the two lists.

    You could also use the Enumerable.Except() method to easily check if one of your lists is a subset of another : 

    // This checks if a your second List is a subset of your first List
    bool isSubset = !list2.Except(list1).Any();

    You can see an interactive example here.

    Friday, October 10, 2014 11:02 AM
  • User-434868552 posted

    @ramll  please clarify:

    (a)  you are telling us that you already know that "List<string2> is subset of List<string1>".  True? False?

    (b) are you testing whether any String in List<String> string2 is also in List<String> string? or

         are you testing whether all Strings in List<String> string2 is also in List<String> string?

    Friday, October 10, 2014 11:56 AM
  • User1655374113 posted

    a)  you are telling us that you already know that "List<string2> is subset of List<string1>".  True? False?

    True. I know List<string2> is subset of List,string1>

    (b) are you testing whether any String in List<String> string2 is also in List<String> string? or

         are you testing whether all Strings in List<String> string2 is also in List<String> string?

    I am assigning the Propoerty 'IsChecked" based on the values that are in List<string2>.
    If string in List<string2> matches List<string1>, I want to make IsChecked = true

    Below is my code which I written. I just want to know, is there any effective way to do this.

    foreach (var str in strings2)
    {
       for (int i = 0; i < strings1.Count() - 1; i++)
        {
          if (strings1[i].Name == str.Name)
          {
             strings1[i].IsChecked = true;
           }
        }
    }



    Friday, October 10, 2014 3:16 PM
  • User-484054684 posted

    Hi,

    I believe you would need to check like this: If you use less than symbol, search till Count, if you use less-than-or-equal symbol then count-1. 

    foreach (var str in strings2)
    {
       for (int i = 0; i < strings1.Count(); i++) // Or, i <= strings1.Count()-1
        {
          if (strings1[i].Name == str.Name)
          {
             strings1[i].IsChecked = true;
           }
        }
    }

    Friday, October 10, 2014 3:35 PM
  • User281315223 posted

    Below is my code which I written. I just want to know, is there any effective way to do this.

    foreach (var str in strings2)
    {
       for (int i = 0; i < strings1.Count() - 1; i++)
        {
          if (strings1[i].Name == str.Name)
          {
             strings1[i].IsChecked = true;
           }
        }
    }

    You could just loop through the values that match :

    // Loop through your collection of strings
    foreach(var s1 in strings1)
    {
        // Check the values that are present in the other collection
        s1.IsChecked = strings2.Any(s2 => s1.Name == s2.Name);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2014 3:57 PM
  • User-434868552 posted

    @ramll   

    ramll

    I know List<string2> is subset of List,string1>

    http://en.wikipedia.org/wiki/Subset

    If List<string2> and List<string1> are sets and every element of List<string2> is also an element of List<string1>, then:

    • List<string2> is a subset of (or is included in) List<string1>, denoted by A \subseteq B,

    ramll, if you already know, as you write, "List<string2> is subset of List,string1>", then, why are you bothering to retest the strings in List<string2>?

    what do you gain by retesting your second list; is this not similar to using a calculator to prove that 2+2 equals 4?

    edit:

    see http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer 

    your code:

    List<String> strings1 = new List<String> { "A", "B", "C", "D" };
    List<String> strings2 = new List<string> { "B", "C" };
    foreach (var str in strings2)
    {
       for (int i = 0; i < strings1.Count() - 1; i++)
        {
          if (strings1[i].Name == str.Name)  // ERROR 1.
          {
             strings1[i].IsChecked = true;   // ERROR 2.
           }
        }
    }
    
    // ERROR 1
    // 'string' does not contain a definition for 'Name' and no extension method 'Name'
    // accepting a first argument of type 'string' could be found
    
    // ERROR 2
    // 'string' does not contain a definition for 'IsChecked' and no extension method 'IsChecked'
    // accepting a first argument of type 'string' could be found

    end edit.

    Saturday, October 11, 2014 4:34 PM