Link to home
Start Free TrialLog in
Avatar of suresh pondicherry
suresh pondicherryFlag for United States of America

asked on

Linq remove object from one list by comparing other list

Hi ,
Need quick help. Have 2 object lists in C#

list<obj1> a = new list<obj1>(); list<obj2> b = new list<obj2>();
i.e.
a[0]="test1"     b[0]="hi"
a[1]="test2"     b[1]="hello"
a[2]= "test"      b[2]= "test1"
                          b[3]= "test"

want to remove object in "a list" only if respective obj matched from b list.

kind regards,
Pooja
ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suresh pondicherry

ASKER

Thanks Russ Suter
It worked very well
You could also use the Except and Concat methods; e.g. -
using System;
using System.Linq;

namespace EE_Q29101143
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new[] { "test1", "test2", "test" };
            var b = new[] { "hi", "hello", "test1", "test" };
            Console.WriteLine($"{{ {string.Join(", ", a.Except(b).Concat(b.Except(a)))} }}");
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -User generated image-saige-
Hi it_saige,
It's the best answer.

Kind regards,
Pooja
Avatar of Russ Suter
Russ Suter

Closing as per author comments