0

Store

  1. found items in one List.

  2. not found items in another List. I got first part by doing this

       for(int i = 0; i < list1.Count; i++)
        {
            for (int j = 0; j < list2.Count; j++)
            {
                if (list1[i] == list2[j]))
                {
                    list3.Add(list1[i]);
                    continue;
                }
             }
         }
    

I need the "Second part." There are still better ways to search a list but I preferred this.

shashank99
  • 21
  • 3
  • 2
    Does this answer your question? [Get the difference between two lists using LINQ](https://stackoverflow.com/questions/7347386/get-the-difference-between-two-lists-using-linq) – IndieGameDev Nov 27 '20 at 09:29
  • @MathewHD I think his question is to get items which are not present in the list – Lucifer Nov 27 '20 at 09:34
  • By the way... You may get rid of your **continue** statement there on your solution for the "found" list. Being where it is, it does nothing for you, because after the list3.Add() call, it would "continue" on the inner for loop anyways. – String.Empty Nov 27 '20 at 10:16

3 Answers3

2

you can try

var list3 = list1.Where(x => list2.Any(y => x == y));
var list4 = list1.Except(list3);

The above code will give you IEnumerable<> if you want a List<> just add .Tolist() before semicolon(;)

if you need list4 with items which are uncommon in list1 & list2 then

var list3 = list1.Where(x => list2.Any(y => x == y));
var list4 = list1.Except(list3).Concat(list2.Except(list3));

FootNotes for more reading:

Enumerable.ToList

Enumerable.Except

Lucifer
  • 1,594
  • 2
  • 18
  • 32
2

You can use Linq's Intersect() and Except() for this:

var data    = new List<string>{"A", "B", "C", "D", "E", "F", "G", "H", "I"};
var targets = new List<string>{"C", "D", "E", "F"};

var found    = data.Intersect(targets).ToList();
var notFound = data.Except   (targets).ToList();

Console.WriteLine("Found:     " + string.Join(", ", found));
Console.WriteLine("Not found: " + string.Join(", ", notFound));
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • +1 for simlifying the where condition, [Enumerable.Intersect](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=net-5.0) – Lucifer Nov 27 '20 at 09:50
2

I see the other answers make use of LINQ (which is by far the best way to do this) when it seems to me you are learning and/or studying loops. So if you rather follow the "basics" approach as you did with the "found" list, you can try the following for the "not found" list:

for (int i = 0; i < list1.Count; i++)
{
    bool add = true;
    for (int j = 0; j < list2.Count; j++)
    {
        if (list1[i] == list2[j])
        {
            add = false;
            break;
        }
    }
    if (add == true)
    {
        list4.Add(list1[i]);
    }
}

If your purpose is not simply learning the language and the syntax like I assumed, please go with the other answers that use LINQ, for they are much more professional.

String.Empty
  • 145
  • 13
  • instead of bool value can it be simlified to `if (list1[i] == list2[j])) list3.Add(list1[i]); else list4.Add(list1[i]);` – Lucifer Nov 27 '20 at 10:12
  • @Lucifer That would not work. Each time an element from list1 does not match an element in list2, you would add that list1 element into list4. You would end up with all elements of list1 into list4. Worse, actually, since many of them would be repeated several times. – String.Empty Nov 27 '20 at 10:31