Friday, April 22, 2016

LINQ comes to rescue!


LINQ comes to rescue!

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

Let's first create a simple class






















we have list of cars, like this.





















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.

Here is the solution from LINQ,










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.

No comments:

Post a Comment