didntCthatcoming didntCthatcoming - 4 months ago 7
ASP.NET (C#) Question

Get a filtered list from two lists

I have a listbox where all my factories are loaded into. I also have a selected factories in listbox(1).

Binding Code for all factories in listbox right.

protected void fillWerke()
{
//BindingListPersistable<Liebherr.Hau.Erp.Client> bl = new BindingListPersistable<Liebherr.Hau.Erp.Client>();
AllFactory = new BindingListPersistable<Liebherr.Hau.Erp.Client>();
AllFactory = Manager.LoadClients();
BetroffeneWerkeAll.DataSource = from allF in AllFactory select allF.ClientCode;
BetroffeneWerkeAll.DataBind();
}


What I tried is,

protected void fillWerke()
{
//BindingListPersistable<Liebherr.Hau.Erp.Client> bl = new BindingListPersistable<Liebherr.Hau.Erp.Client>();
AllFactory = new BindingListPersistable<Liebherr.Hau.Erp.Client>();
AllFactory = Manager.LoadClients();
var selectedFac;
var filteredFac;
if(SelectedWerke.Items.Count > 0)
{
selectedFac = SelectedWerke.Items;
filteredFac = AllFactory.Except(selectedFac);
}
BetroffeneWerkeAll.DataSource = from allF in AllFactory select allF.ClientCode;
BetroffeneWerkeAll.DataBind();
}


however, i hope u know what i mean, sorry for my bad english

Answer

Try instead of:

BetroffeneWerkeAll.DataSource = from allF in AllFactory select allF.ClientCode;

This:

 BetroffeneWerkeAll.DataSource = from allF in filteredFac select allF.ClientCode;

When you perform Except it returns the "input list except items in second list" and not sets the first list.


Not sure but it seems to me that selectedFac is of the type of ClientCode and not of an item in AllFactory. If that is the case you should use:

var result = AllFactory.Where(item => 
        !selectedFac.Any(filteredValue => filteredValue == item.ClientCode)).ToList();