开发者

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

开发者 https://www.devze.com 2023-03-05 15:34 出处:网络
I have a ObservableCollection<AnyClass> collection. This collection has data like ID, first name, second name开发者_开发技巧, address, pin, city, Salary and Description.

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.


Try this:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消