且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用LINQ更新列表中的值

更新时间:2023-02-13 11:18:54

LINQ用于查询,而不用于更新数据.使用LINQ获取要修改的项目,然后在foreach循环中对其进行修改:

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;
}

演示.