suresh pondicherry
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
}
}
Which produces the following output --saige-
ASKER
Hi it_saige,
It's the best answer.
Kind regards,
Pooja
It's the best answer.
Kind regards,
Pooja
Closing as per author comments
ASKER
It worked very well