Daniele Sartori
Daniele Sartori

Reputation: 1703

How can i select elements from an IEnumerable to store them in another IEnumerable?

I have built a custom User Control, that has as input an IEnumerable, and it should return also an IEnumerable. This is to have a flexible control that can receive any collection of objects. Here is some code snippets to help you understand my problem:

I was able to have my control work except for this part that Build the SelectedItems property

foreach(string s in appo)
{
  IEnumerator en = ItemsSource.GetEnumerator();
  while (en.MoveNext())
  {
      var val = en.Current;
      Type type = val.GetType();
      PropertyInfo property = type.GetProperty(DisplayMemberPath);
      if (property != null)
      {
          string name = (string)property.GetValue(val, null);
          if(name == s)
          {
              // need something here          
          }
      }
   }
}

Basically inside the if i have checked that the current element of the IEnumeratoren must be included in the SelectedItem. The problem is that i don't know how i can include this element in the output (that is SelectedItems).

I am open also to different approach, if you have better ideas

Upvotes: 0

Views: 138

Answers (1)

Tim Rutter
Tim Rutter

Reputation: 4679

The issue is that IEnumerable does not have the ability to add items. You therefore need to change it to be something that has an Add method, IList for example, or ICollection<object>.

Upvotes: 3

Related Questions