Hello everyone, today we are going to discuss the reasons why LINQ can be addictive. I’m a .Net developer and ever since I learned LinQ and mastered it, I just can’t give up on it. I feel like saying LINQ is one the best addition made to an Object Oriented Programming language. If you are a .Net developer too, I’m chances are you feel that way too every time you use LinQ😁.

What is LINQ ?

LINQ, abbreviation for (Language Integrated Query). And as stated in this article:

LINQ is a Microsoft programming model and methodology that essentially adds formal query capabilities into Microsoft .Net based programming languages. LINQ offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes from its ability to apply the same query to an SQL database, a DataSet, an array of objects in memory and to many other types of data as well.

LINQ makes working with data extremely easy, it makes you code cleaner. And it works for a wide variety of data sources. If it was up to me, then every programming language would have had their own implementation of LINQ 😁.LINQ is just so sweet and here are 5 reasons why LINQ can be addictive.

5 Reasons Why LINQ Can Be Addictive

I ) LINQ is simple and Clean

Working with lINQ is very easy since it is similar to querying data using normal english words (Just like SQL syntax). Not only does LINQ drastically reduce the number of lines of code you write for most operations, it also makes you code cleaner and easy to read and maintain. LINQ has two main syntax for querying data. The SQL like syntax, and extension methods syntax. Each of them could be used interchangeably. Or in certain scenarios, both could be use.

II ) LINQ syntax is unique no matter the data source.

At first when .Net was still young (and dinosaurs where still alive… 😁), where your data was found determined how you would access the data and which tools you will use. If your data was stored in an SQL Server database, you had to use the query methods provided by ADO.net to access data with SQL commands. If Your data was in an XML file, you had to use other methods provided by a different API to query data from the XML document. And so on. But LinQ came in and provided a consistent API to query data from these sources and many other sources of data. Thus, making developers job easier since they leverage their LINQ queries on a wide variety of data sources.

III) Reactive LINQ

With .Net’s Reactive Extensions (RX), you have a convenient way of listening for events, handle errors, set timeouts and a lot more. It provides a convenient way of declaring call backs and manage execution of asynchronous code. RX extends LINQ and consequently, you have the ability to turn a LINQ Query in an observable sequence then, subscribe a function as an observer of that sequence. This does not only make your code beautiful, but makes maintaining the code easy. Though You can use RX without any LINQ I think it is better to use RX with LINQ. In my opinion, once you know how to leverage this feature, giving up on it is difficult (Why will you even want to give up on it ?).

Example:

//Observe any time the TextChanged event is fired on this entry, and delay it by 3 seconds textEntryObservable = Observable.FromEventPattern<EventHandler<TextChangedEventArgs>, TextChangedEventArgs>( x => entry.TextChanged += x, x => entry.TextChanged -= x ) .Delay(TimeSpan.FromSeconds(3)) .Select(args => args.EventArgs.NewTextValue); //Subscribe to this obervable textEntryObservable.Subscribe(text => { //Do something with this text }); 1 2 3 4 5 6 7 8 9 10 11 12 13 //Observe any time the TextChanged event is fired on this entry, and delay it by 3 seconds textEntryObservable = Observable . FromEventPattern < EventHandler < TextChangedEventArgs > , TextChangedEventArgs > ( x = > entry . TextChanged += x , x = > entry . TextChanged -= x ) . Delay ( TimeSpan . FromSeconds ( 3 ) ) . Select ( args = > args . EventArgs . NewTextValue ) ; //Subscribe to this obervable textEntryObservable . Subscribe ( text = > { //Do something with this text } ) ;

The code above changes the TextChangedEvent into an observable, and delay its execution by 3 seconds. Then uses LINQ to get only the NewTextValue each time the event is fired. Then below that, we subscribe a lambda function as the observer which will perform the appropriate tasks with the text when it is entered. This demo is functional on Xamarin.Forms you can get more information about Reactive programming and MVVM in Xamarin.Forms with ReactiveUI by following this Link.

IV) The complex becomes simple

When working with data, you will certainly need to make complex operations on that data. For example, you might want to sort data, filter or project it in a different data structure. You may also want to do complex operations which require joins. Doing this with normal code can be a little bit cumbersome. Imagine you were writing code to perform each of these tasks explicitly, not only will it waste a lot of time, but reading that code later would not be easy at all. LINQ simplifies these tasks. You can perform such tasks with LINQ in just a few lines of code. Yet another reason why LINQ can be addictive.

Sorting and Projecting

List<Man> men = new List<Man> { new Man{ Name = "John", Age = 30}, new Man{Name = "Peter", Age = 21}, new Man{Name = "George", Age = 40} }; //REturns the name of the oldest man var theNameOfTheOldestMan = men.OrderByDescending(man => man.Age).Select(man => man.Name).First(); 1 2 3 4 5 6 7 8 9 List < Man > men = new List < Man > { new Man { Name = "John" , Age = 30 } , new Man { Name = "Peter" , Age = 21 } , new Man { Name = "George" , Age = 40 } } ; //REturns the name of the oldest man var theNameOfTheOldestMan = men . OrderByDescending ( man = > man . Age ) . Select ( man = > man . Name ) . First ( ) ;

V) LINQ is suitable for diverse scenarios

While programming, there are simple tasks which you may want to perform. Such as summing up all the values in a list, iterating through a list and several other operations. Performing such simple tasks normally may require several lines of code. Though this is not always advised, LINQ can be used to performs these simple operations. Here are some examples of this.

For each loop in LINQ

List<int> myListOfNumbers = new List<int>(); myListOfNumbers.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); myListOfNumbers.ToList().ForEach(c => { //Perform apropriate action }); 1 2 3 4 5 6 7 8 List < int > myListOfNumbers = new List < int > ( ) ; myListOfNumbers . AddRange ( new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ) ; myListOfNumbers . ToList ( ) . ForEach ( c = > { //Perform apropriate action } ) ;

Summing all items in a list with LINQ

var sum = myListOfNumbers.Sum(); 1 var sum = myListOfNumbers . Sum ( ) ;

Conclusion

Ever since I learned LINQ, I haven’t stopped using it. I’m like a LINQ junky 🤤. In my opinion, every .Net developer should master LINQ since it has several advantages. Above I stated a few reasons why LINQ can be addictive, and these points are open to criticism. There is a comment section below, feel free to share your opinion.

You may also be interested about how to use ReactiveUI MVVM framework on Xamarin Forms Here.

If this post was useful to you, please share it and follow me on Twitter, Github or like my Facebook page.

Like this: Like Loading...

Like this: Like Loading...

Follow me on social media and stay updatedFollow me on social media and stay updated