Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Hello 2 All...

i have 2 Generic Lists of the same type... FirstList Contains all the possible values getting from SupplierTable.. while the SecondList conatains some selected values of suppliers getting from ItemTable( i.e All those Suppliers for a specific item).....

Now i want to exract all values from FirstList which do not exist in the Secondlist????

what should i do for this???

Muhammad Tufail...
Posted
Updated 28-Jun-11 5:48am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 12:16pm    
I don't know your code but have a feeling you do it too late. You could use proper SQL query to obtain the lists with the subsets of values you want, in first place.
The problem with list it trivial though.
--SA
Muhammad Tufail 1979 28-Jun-11 13:57pm    
i have 2 quries one to bring all the suppliers in the firstList and the seconde query bring all the items with (suppliersList ... and this is my seconed list)

now i want to separate the values of second list from the first and remaing to show in a listbox.....

You can use Contains to do this. I know it seems counter-intuitive, but if you invert Contains, it becomes not contains. So, you could have code that looks like this:
C#
var myList = from c in firstList
    where !secondList.Contains(c.supplierId)
    select c; 
 
Share this answer
 
Comments
Muhammad Tufail 1979 28-Jun-11 11:57am    
Sorry Sir.... it doesnot working ..

it gives a problem that....... Contains method determine wether the value exists or not and return a boolean value...


i dont undersand but its not working plzzzzz.....
Pete O'Hanlon 28-Jun-11 12:14pm    
That's right - it returns a boolean, but you are returning an IQueryable here. You can easily convert it to a list by evaluating myList.ToList(); or you could bracket the from bit like this:

var myList = (from c in firstList
where !secondList.Contains(c.supplierId)
select c).ToList();
Morl99 28-Jun-11 12:10pm    
Use my method instead. If you want to preserve the original list, copy it beforehand.
Muhammad Tufail 1979 29-Jun-11 1:27am    
i had tried all the following code but not got the right solution..


remainingSuppliers = from sp in suppliers where !it.Suppliers.Contains (sp.ID ) select sp;
var list = from sp in suppliers where !it.Suppliers.Contains(sp) select sp;
var list = (from sp in suppliers where !it.Suppliers.Contains(sp) select sp).ToList();

remainingSuppliers = suppliers.Except(it.Suppliers).ToList ();

foreach (Supplier ss in remainingSuppliers)
{
leftListBox.Items.Add(ss.Name);
}

Muhammad Tufail 1979 28-Jun-11 13:52pm    
Now it does not give any error .... but it does not remove all those items which i want to remove from the firstlist......

first list conatain all those items which are in second list......

sorry still i didnt got the solution...
Just iterate over the second List and remove the items you have there from the first list. Maybe like this:

List<T> firstList;
List<T> secondList;
foreach (T t in secondList){
  firstList.remove(t);
}


That should do the trick (If the items are truly equal). Of course you have to plugin your own types and variable names.

Now if the items are not really equal you need to check that for yourself. Try something like this
foreach (DataType supplierToRemove in secondList) {
  int supplierID = supplierToRemove.ID;
  DataType delete = null;
  foreach (DataType supplier in firstList){
    if(supplier.ID == supplierID){
      delete = supplier;
      break;
    }
  }
  if (delete != null)
    firstList.remove(delete);
}


Be aware that I don't know your code! You have to adapt this solution to your needs. If you do not have an ID, use your Primary Key of the Table instead.
 
Share this answer
 
v5
Comments
Muhammad Tufail 1979 28-Jun-11 13:53pm    
Now it does not give any error .... but it does not remove all those items which i want to remove from the firstlist......

first list conatain all those items which are in second list......

sorry still i didnt got the solution...
Could you please try following, it might help you to sort out the issue,

C#
var firstList = new List<string>() { "A", "B", "C" };
var secondList = new List<string>() { "A", "B" };
var resultList = firstList.Except(secondList);


Thanks :)
 
Share this answer
 
Comments
Muhammad Tufail 1979 29-Jun-11 1:27am    
i had tried all the following code but not got the right solution..


remainingSuppliers = from sp in suppliers where !it.Suppliers.Contains (sp.ID ) select sp;
var list = from sp in suppliers where !it.Suppliers.Contains(sp) select sp;
var list = (from sp in suppliers where !it.Suppliers.Contains(sp) select sp).ToList();

remainingSuppliers = suppliers.Except(it.Suppliers).ToList ();

foreach (Supplier ss in remainingSuppliers)
{
leftListBox.Items.Add(ss.Name);
}
Mohammad A Rahman 29-Jun-11 1:46am    
Could you please give a example of Test data for suppliers and it.Suppliers and as well the output your getting. :)
Muhammad Tufail 1979 29-Jun-11 2:00am    
i have 4 Suppliers in database ( tabel name is Suppliers) and
it.suppliers ( mean item.suppliers) this list contains 2 suppliers at the moment... i mean to say that ( every item has its own suppliers)....

now i want that to exclude these 2 suppliers from the suppliers List and the remaining suppliers should display in the listbox with the name of leftListBox.
Mohammad A Rahman 29-Jun-11 2:09am    
I am assuming suppliers list contains {"A","B","C","D"}. Based on what you said, it.suppliers I guess it is a list with {"A","B"} then simply suppliers.Except(it.Suppliers) should give us {"C","D"}. In another case if it.suppliers is a single item (assumption) then suppliers.Except(List Of it.Suppliers). :)
Muhammad Tufail 1979 29-Jun-11 2:16am    
yes exactly plz the first case is my case of problem plzzzzzz
i m very thankfull to all the codeproject team, and all those who provide me alot of help in solving the problems.. i got the solution of that problem and now i m going to improve myself futher more in this field...

thanx

Muhammad Tufail
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900