Jason
Jason

Reputation: 227

How can I compare two Lists and create another list where the match?

I have the following two lists:

IEnumerable<bool> a =      True True True False
List<bool> b = True True False False

I need to create another list that shows true if the element of list a and list b match. List a and b will always be populated and have the same number of elements. So for example the output would be:

List<bool> c = True True False True

Is there a simple way I can do this? Would I have to use LINQ? I guess I could do it be iterating through an array but I am hoping there's a simpler way.

Upvotes: 2

Views: 1703

Answers (6)

gaurawerma
gaurawerma

Reputation: 1826

You can use IComparable and compare two lists and get the result in the ghosts list.

Upvotes: 0

Saeed Amiri
Saeed Amiri

Reputation: 22555

you can use SequenceEquals:

lst1.SequenceEquals(lst2);

Also if u need element by element check u can use Select clause (if you have lists or arrays not enumerable):

lst1.Select((x,index)=>lst2[index] == x);

Upvotes: 1

Ian G
Ian G

Reputation: 30234

Here is a really easy way to go about it without using LINQ, but using good old fashioned for loops :-)

In this method you have to convert your IEnumerable<bool> into a List<bool>, but that is trivial.

        IEnumerable<bool> z = new List<bool> { true, true, true, false };
        var a = new List<bool>(z);
        var b = new List<bool> { true, true, false, false };
        var result = new List<bool>();

        for (int i = 0; i < b.Count ; i++) {
            result.Add(a[i] && b[i]);
         }


        foreach (var r in result)
                Console.WriteLine(r);

Upvotes: 0

Adnan Bhatti
Adnan Bhatti

Reputation: 3480

Use bitwise operators, you can hand code each element to compare or use loop.

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<bool> a = new List<bool>() { true, true, true, false };
        List<bool> b = new List<bool>() { true, true, false, false };
        List<bool> answers = new List<bool>();
        for (int i = 0; i < b.Count; i++)
        {
            answers.Add(a.ElementAt<bool>(i) & b.ElementAt<bool>(i));

        }

         foreach (bool B in answers)
            Console.WriteLine(B);

         Console.ReadKey();

     }
 }

Upvotes: 0

Rajiv Makhijani
Rajiv Makhijani

Reputation: 3651

There are two ways to go about this:

1) If there is not a lot of data, just loop through and create a new List

2) Implement IEnumerable and create your own view into the two lists (take them as the constructor), and implement the methods such that they use the underlying lists (i.e. for Current : get, do the comparison between the two lists in realtime)

Upvotes: 0

Chris Schmich
Chris Schmich

Reputation: 29476

If you're using .NET 4, you can use LINQ through IEnumerable.Zip to "zip" the two lists together into a third result list. Zip operates on coordinating elements in both lists to produce a third list.

The second parameter is a function defining what you want to do with each pair of elements.

var c = a.Zip(b, (p, q) => p == q).ToList();

Upvotes: 6

Related Questions