LINQ comes to rescue!

Today, I’m going to explain a very small but tricky code part, day to day we use this, but never give it a second glance, because of that we miss this in many places :)

Let’s first create a simple class

Car class

we have list of cars, like this.

List of cars

I want to set Milege property as ‘100’ when year is ‘2015’
so, what’s the method comes into your mind ?
for loop, foreach or while loop, Iterations are not so good when it comes to the performance.

With the help of LINQ, we can perform the filtering and modification both in just one line :)

In LINQ world, we have many operations to filter, to projection, to modify and many more.

Solution from LINQ,

LINQ query to filter and modify list values.

Yes, It’s difficult to read the code though, But if you have performance concerns, and want to write the program with few lines of code, You can get use of this LINQ query.