SuicideSheep
SuicideSheep

Reputation: 5550

C# Lambda to filter list based on existence on another list

List A: 3,5,5,5,7,9
List B: 3,5

Both of the list are the same type and those values are from a field ID. My objective is to construct a forloop that will return me 7,9 because 7,9 is not existed in List B.

I've tried the following but no luck:

int counter = 0;
foreach(var item in ListA.Where(x=>ListB.Any(b=>x.ID != b.ID)))
{
    counter++;
    //Here I should perform operation with item that having ID 7 and 9
}

Updates:
Using a except method in the above case, counter will still return me 4 simply because each of the 5 in ListA are different object eventhou they are sharing the same ID. My ultimate objective is to have the counter as 2 irregardless whether the object is the same or not. As long as the ID of object in ListA is 3 or 5, I would wanna exclude it.

Upvotes: 2

Views: 3504

Answers (6)

Raj Chaurasia
Raj Chaurasia

Reputation: 1974

Except method can be used when both List are of same type. If Type is different. We can use like this.

var outPut = _employees.Where(i => _employeeExtensions.Any(j => i.EmpId == j.EmpId));

Upvotes: 1

pcnThird
pcnThird

Reputation: 2372

I think you want to get the items in a list where the items' IDs are different:

Example that I put together in LinqPad:

void Main()
{
    List<Person> a = new List<Person>()
    {
        new Person { ID = 1 },
        new Person { ID = 2 },
        new Person { ID = 3 },
    };

    List<Person> b = new List<Person>()
    {
        new Person { ID = 1 },
    };

    var c = a.Where(x => b.Any(bprime => bprime.ID != x.ID));

    foreach(var item in c)
    {
        Console.WriteLine(item.ID);
    }
}

class Person
{
    public int ID { get; set; }
}

Output:

2

3

This works similar to the Except method but this will check the elements' properties.

Upvotes: 0

Rex
Rex

Reputation: 2140

it should be "ALL", or "Not Any"

foreach(var item in ListA.Where(x=>ListB.All(b=>x.ID != b.ID)))
{
    //Here I should perform operation with item that having ID 7 and 9
}

update:

As you actually want to have distinct result from A except B, so, you can do either:

foreach(var item in ListA.GroupBy(m=>m.ID).Where(x=>ListB.All(b=>b.ID != x.Key)))
{
    counter ++;
    Debug.writeline(item.Key);
}

or

foreach(var id in ListA.Select(x=>x.ID).Distinct().Except(ListB.Select(y=>y.ID)))
{
    counter++;
}

note: all untested - i have no compiler with me for the moment.

Upvotes: 2

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Change your query like this:

foreach(var item in ListA.Where(x=> !ListB.Any(b => x.ID == b.ID)))

And it should work fine.

Upvotes: 3

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Try This:

List<int> listA=new List<int>(new[]{ 3,5,7,9});
List<int> listB=new List<int>(new[]{ 3,5});

var items=(from a in listA
           select a).Except(from b in listB
           select b);

foreach(var item in items)
{
    Console.WriteLine(ll);
}

Output:

7  
9

Upvotes: 1

JaredPar
JaredPar

Reputation: 754545

Just use the Except extension mtehod

foreach (var item in ListA.Except(ListB)) {
  ...
}

Upvotes: 4

Related Questions