Query a winforms control with CheckedListBoxItemCollection.AsQueryable().Provider.CreateQuery(

StackOverflow https://stackoverflow.com/questions/3587449

문제

I need to query a winforms control(CheckedListBoxControl) having a CheckedListBoxItemCollection which I want to query for a certain Id that is within the property "Value" of the 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));

Why says Visual Studio that the IEnumerable or the IEnumberable namespace of it can not be found? When I use "var" instead then I can compile my code. But the boss in my company forbide my to use var...

도움이 되었습니까?

해결책

CheckListBox.Items only implements IEnumerable, not IEnumerable<T>. You get the overload of AsQueryable() that returns IQueryable (not the generic one). Which only has the Cast and OfType extension methods.

Cast the items back from object to Department. Like this:

var q = checkedListBox1.Items.Cast<Department>().AsQueryable().Where((d) => d.Id == 1);

You don't need AsQueryable() anymore btw.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top