views:

19

answers:

1

Hello,

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...

A: 

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.

Hans Passant
you misunderstood one thing: the CheckListBoxControl is not databound to Department entities. The CheckedListBoxControl gets its Items manually added from a XML file.The "objects" in the departmentList and the CheckedListBoxControl do not have all the same properties. The only property which is the same is the Id I want to search for.But maybe I also misunderstood you.I have updated my init post with a code sample and a relating question :P
msfanboy
Well, I had to do some guessing without a full code sample. Sounds like my answer was useful to you. Why didn't you mark it as such?
Hans Passant
because I still wanted to wait for the final answer on my updated question else no problem Hans!
msfanboy
Please unmark the answer so I can delete it. Giving helpful answers is kinda important for most anybody contributing to SO.
Hans Passant
now I am confused. Your answer was ok for me. The updated question is not belonging to the origin question. To answer it would just be the creme on the ice... but it does not depend on accepting the whole issue as solution. So when you do not answer it you still got the solution.
msfanboy