Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update value in a List using LINQ

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

class Student
{
    private string name;
    private int marks;
    public string Name { get; set;}
    public int Marks { get; set; }
    public Student(string name, int marks)
    {
        Name = name;
        Marks = marks;
    }
}

List<Student> myList = new List<Student>();
myList.Add(new Student("John", 10));
myList.Add(new Student("Tom", 20));

Now I want to update the list using LINQ such that only marks of John gets updated. I am using the following syntax:

myList.Where(w => w.Name == "Tom").Select(w=> { w.Marks = 35; return w});

But this doesnt update data in myList. Can someone tell me where am I going wrong.

like image 405
user3625024 Avatar asked May 05 '15 14:05

user3625024


People also ask

How to update value in list using LINQ?

Show activity on this post. I have a list which I want to update using LINQ. class Student { private string name; private int marks; public string Name { get; set;} public int Marks { get; set; } public Student(string name, int marks) { Name = name; Marks = marks; } } List<Student> myList = new List<Student>(); myList.

What is Linq in C# with example?

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.


3 Answers

Try:

myList .Where(w=> w.Name  == "dTomi").ToList().ForEach(i => i.Marks  = 35);
like image 189
SHIBIN Avatar answered Sep 21 '22 08:09

SHIBIN


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.

like image 27
Sergey Kalinichenko Avatar answered Sep 22 '22 08:09

Sergey Kalinichenko


As others have pointed out, LINQ is for querying data, it is not for doing updates.

You should iterate your list, and modify your values like:

foreach (var student in myList)
{
    if (student.Name == "Tom")
    {
        student.Marks = 35;
    }
}

Or

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

Whatever you think better conveys the intent use that.


but here is an interesting thing:

If you have a statement like:

myList.Where(w => w.Name == "Tom").Select(w => w.Marks = 35).ToList();

Without assigning the result back to myList the above query will modify the value in the original list. Remember, it is a side effect and it is not the proper way to update. This is modification can be explained by reference parameter being passed to a method and getting modified there. But Importantly, this should always be avoided. It is a bad practice and could lead to really confusing code. Use LINQ for querying only.

like image 44
Habib Avatar answered Sep 22 '22 08:09

Habib