Kashmir Singh
Kashmir Singh

Reputation: 1

How to change a value form ObservableCollection<AnyClass> with LINQ?

I have a ObservableCollection<AnyClass> collection. This collection has data like ID, first name, second name, address, pin, city, Salary and Description.

I want to change the description of this collection whose id is 10 or 12 or any ID.

Thanks in advance.

Upvotes: 0

Views: 2237

Answers (2)

Gopi
Gopi

Reputation: 256

Although it is old question, someone may find my answer useful.

You can do this by using LINQ to find necessary item using appropriate predicate and then modify.

var selectedItem = myCollection.FirstOrDefault(x => x.Id == 10);

if (selectedItem != null)
{
   selectedItem.Description = "Do something";
}

Hope it helped to someone.

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174397

Try this:

foreach(var item in collection.Where(x => x.ID == 10))
{
    item.Description = newDescription;
}

Upvotes: 6

Related Questions