How to update value in a List using LINQ

后端 未结 9 940
天命终不由人
天命终不由人 2020-12-31 14:45

I have a list which I want to update using LINQ.

class Student
{
    private string name;
    private int marks;
    public string Name { get; set;}
    pub         


        
9条回答
  •  北海茫月
    2020-12-31 15:19

    LINQ is for querying, not for updating the data. Use LINQ to get the items that you want to modify, and then modify them in a foreach loop:

    foreach ( var tom in myList.Where(w => w.Name == "Tom")) {
        tom.Marks = 35;
    }
    

    Demo.

提交回复
热议问题