Query un controllo WinForms con CheckedListBoxItemCollection.AsQueryable (). Provider.CreateQuery (
-
01-10-2019 - |
Domanda
Ho bisogno di interrogare un controllo WinForms ( CheckedListBoxControl ) con un CheckedListBoxItemCollection , che voglio query per un certo ID che è all'interno della proprietà "Valore" della CheckedListBoxItem .
CheckedListBoxItemCollection items = myCheckedListBoxControl.Items;
foreach(Department dep in departmentList)
{
bool isDepExisting = items.AsQueryable().Where( the .Where clause does not exist );
// How can I query for the current dep.Id in the departmentList and compare this dep.Id with every Item.Value in the CheckedListBoxControl and return a bool from the result ???
if(!isDepExisting)
myCheckedListBoxControl.Items.Add( new CheckedListBoxItem(dep.id);
}
UPDATE:
IEnumberable<CheckedListBoxItem> checks = items.Cast<CheckedListBoxItem>().Where(item => item.Value.Equals(dep.InternalId));
Perché dice Visual Studio che l'IEnumerable o lo spazio dei nomi IEnumberable di esso non può essere trovato? Quando uso "var", invece, allora posso compilare il mio codice. Ma il capo della mia azienda forbide mia per usare var ...
Soluzione
CheckListBox.Items implementa solo IEnumerable, non IEnumerable<T>
. È possibile ottenere il sovraccarico di AsQueryable () che restituisce IQueryable (non quella generica). Che ha solo il cast e metodi di estensione OfType.
Fusioni gli articoli di nuovo da oggetto a Department. In questo modo:
var q = checkedListBox1.Items.Cast<Department>().AsQueryable().Where((d) => d.Id == 1);
Non è necessario AsQueryable () più BTW.