Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 23, 2015 6:50 AM
Hi,
I have one ObservableCollection and i have the some values in this like below.
ObervableCollection<Model> myCollection = new ObservableCollection<Model>();
The above mentioned model contains the complex property.
Model class definition:
Public class Model
{
public int Number;
//complex property
Public Employee Name;
}
Public class Employee
{
public string FirstName;
}
Model m = new Model();
m.Number = 12;
m.Name = new Employee();
m.Name.FirstName = "Name1;
myCollection.Add(m);
m= new Model();
m.Number = 15;
// here i am not set the value to complex properties.
myCollection.Add(m);
IEnumerable records = (myCollection as IEnumerable).OfQueryable();
Now i need to remove the item from above IEnumerable records and the condition to remove the items If the complex property having null. ( In the above, i am not set the value to second record). so in my case i need to remove one record from the records.
I will not use the temporary collection to remove this.
I will not remove that record from ObservableCollection itself.
Thanks in Advance, Please help me.
Thanks,
Elavarasan M
All replies (6)
Thursday, June 25, 2015 6:22 PM âś…Answered
>>Could you please share how to remove the item from IEnumerable without using the temporary collection as i mentioned above.
Not possible. You cannot remove something from an IEnumerator because an IEnumerator is just en enumerator that supports a simple iteration over a non-generic collection. It is not a collection that you can remove something from. It just enumerates a collection.
By design IEnumerable implements for...each syntax.
Wich includes that the access is read only (to the collection elements themself). The moment you remove something from the underlying collection, stuff starts to get real wonky and wierd with IEnumerable
Wich is why you need a seperate copy of the data to itterate over to do your check and then remove the data from the real collection.
Also note that this looks like a WPF question (ObservableCollection), wich has a seperate main forum.
Removing or adding too much stuff from/to an OC while it is bound can cripple GUI performance. It is often better to remove the collection from the GUI entirely while you do lots of add & remove opeartions, then expose it again.
Tuesday, June 23, 2015 3:22 PM
You need to create a shallow copy of the observable collection first (to retain the data in original collection).
ObservableCollection<Model> records = new ObservableCollection<Model>(myCollection);
The complete example is...
ObservableCollection<Model> records = new ObservableCollection<Model>(myCollection);
//get items
var nullItems = records.ToList().Where(p => p.Name == null).ToList();
//iterate and remove
foreach (var item in nullItems)
{
records.Remove(item);
}
Hope this helps.
Anirban Bhattacharya
Thursday, June 25, 2015 3:36 PM
Hi Anirban,
Thank you for your reply.
Could you please share how to remove the item from IEnumerable without using the temporary collection as i mentioned above.
Thanks,
Elavarasan M
Thursday, June 25, 2015 3:57 PM | 1 vote
>>Could you please share how to remove the item from IEnumerable without using the temporary collection as i mentioned above.
Not possible. You cannot remove something from an IEnumerator because an IEnumerator is just en enumerator that supports a simple iteration over a non-generic collection. It is not a collection that you can remove something from. It just enumerates a collection.
You need to remove items from the ObservableCollection without casting it to an IEnumerable. Or you could cast it to an IList<Model> since an IList<T> supports removal of element (an IEnumerable does not):
IList<Model> records = myCollection;
for (int i = 0; i < records.Count; ++i)
{
records.RemoveAt(i);
}
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
Thursday, June 25, 2015 4:02 PM
I don't believe you can. Not using IEnumerable anyway. The whole point of IEnumerable is for you enumerate a collection on the basis that the collection will not change while you are enumerating.
If you have a collection that you want to remove items from then you cannot do that via IEnumerable. You must access the underlying collection object directly or using some other interface (assuming it has some kind of 'Remove' method) and then be careful how you intend to loop around it.
Or use Anirban's suggestion above.
Thursday, June 25, 2015 6:06 PM
There are two other approaches.
1) LINQ, except this does kind of use a temporary collection. The pattern is explained here:
var nullItems = records.Where(p => p.Name == null).ToList();
var ItemsLeft = records.RemoveAll(x => toRemove.Contains(x));
I'm not sure why you want to remove something in just one step, but you could filter the observablecollection so it remains intact and a collectionview filters it so the user sees whatever subset you like:
http://social.technet.microsoft.com/wiki/contents/articles/26673.aspx#Filtering
Note that there is a sample which illustrates this approach with working code.
Hope that helps.
Technet articles: WPF: Change Tracking; All my Technet Articles