LINQ - Exclude items in one list from another

J

Jennifer Mathews

I have a problem I can't seem to solve.
I need a list of vendors that a customer does NOT have in their list.

using System.Linq;
using System.Linq.Expressions;

Example List Data to the classes below
------------
LIST Supplier Class
101 , SupplierName1
102 , SupplierName2
103 , SupplierName3
104 , SupplierName4
105 , SupplierName5

LIST Customer Class for CustomerId = 9999
9999 , CustomerName1 , supplierBase=(101 , SupplierName1)
9999 , CustomerName1 , supplierBase=(104 , SupplierName4)
9999 , CustomerName1 , supplierBase=(105 , SupplierName5)


public class Supplier
{
public virtual int SupplierId;
public virtual string SupplierName;
}

public class Customer
{
public virtual int CustomerId ;
public virtual string CustomerName;
public virtual SupplierBase supplierBase { get; set; } << contains SupplierId and
other info
}

I created a list of ALL vendors:
var vListSupplier = (from suppliers in Session.Linq<Supplier>()
select suppliers).ToList();


I created a list for a SPECIFIC customer and the vendors they use:
var vListCustomer = (from customers in Session.Linq<Customer>()
where customers.CustomerId = 9999
select customers).ToList();

I can easily get a list of VENDORS that that a specific customer uses:
var oLst2 = (From a In vListSupplier
Join b In vListCustomer On a.SupplierId Equals
b.supplierBase.SupplierId
Select a).ToList();

HEY ... it might seem easy to you ...
but it took me a day to get this far. :)
Then it took me another day
NOT to be able to figure-out a solution to the following.

I need a list of vendors that a customer does NOT have in their list.
(Not in the vListCustomer list.)

I just do not know what I am missing. I have tried example
after example on the web but most of the examples show
the LISTs being compared as exactly the same.

I have used the LINQ "Except" function but that requires
the objects be identical in structure so it doesn't seem to help.

I hope the above was understandable. Can you please help?

Thanks
 
A

Alberto Poblacion

Jennifer Mathews said:
[...]
I have used the LINQ "Except" function but that requires
the objects be identical in structure so it doesn't seem to help.

Why not? If I have understood correctly what you are doing, the objects
in both lists should be identical in structure: On the one hand you have the
list of all suppliers. On the other, you have the list of suppliers used by
one customer. Both lists contain objects of tye "Supplier". So applying the
Except function should provide the solution that you are seeking.
 
M

Michael C

Alberto Poblacion said:
Jennifer Mathews said:
[...]
I have used the LINQ "Except" function but that requires
the objects be identical in structure so it doesn't seem to help.

Why not? If I have understood correctly what you are doing, the objects
in both lists should be identical in structure: On the one hand you have
the list of all suppliers. On the other, you have the list of suppliers
used by one customer. Both lists contain objects of tye "Supplier". So
applying the Except function should provide the solution that you are
seeking.

I think the problem is the 2 lists contain the same type of object but
different instances. There are 2 solutions to this. You can override Equals
(and GetHashCode) in the objects or you can create an IEqualityComparer and
pass an instance of that into Except.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top