2

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.

6 Answers 6

4

Just use the Except extension mtehod

foreach (var item in ListA.Except(ListB)) {
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please help take a look at my updated question. Sorry for the lack of information given earlier.
@Mr.SuicideSheep even if ListA has duplicate elements,Except method should work.Are you sure you try it correctly?
3

Change your query like this:

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

And it should work fine.

Comments

2

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.

5 Comments

Please help take a look at my updated question. Sorry for the lack of information given earlier.
i think the solution i give above (using All) should yield what you wanted to achieve, right? (even though i have not tested it...)
or you can use: foreach(var item in ListA.Where(x=>!ListB.Any(b=>x.ID == b.ID)))
sorry did not look at your update carefully - actually you want distinct output, see my update above.
The correct answer is using the combination of Distinct and Except. For your GroupBy way, it's not achieving objective. Thanks for your answer!
1

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

4 Comments

query syntax seems highly unnecessary here :)
@Selman22: yes dear i realized it now and frankly speaking i don;t have idea about lambda expressions, is it easy to learn? if you can, please give me some references.
Here is the msdn reference, though you don't need to use lambda statements here :) just ListA.Except(listB) should be sufficient
@Selman22: Thanks Dear :) , yes you are right @JaredPar has the same answer :)
1

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));

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.