LINQ Aggregates Part 1: LINQ 101

LINQ Aggregates Part 1: LINQ 101

·

5 min read

LINQ stands for Language Integrated Query, it is a built-in language construct that was released as a major part of the .Net Framework 3.5 in 2007.

LINQ is built into the .Net infrastructure, this implies both C# and VB. NET have LINQ built into the language respectively.

LINQ is often used to query data sources such as collections, ADO. NET, XML docs, Entity Framework and even MSSQL server & databases.

You can read the official doc to get more information about LINQ and its capabilities.

I want to show you using simple examples how powerful LINQ is and how to utilize them when querying data from different sources whether they be collection or databases.

This article will get you up and running with LINQ in an example driven fashion. Lets look up some examples of how LINQ has made writing code a lot more convenient in C#.

Without LINQ,

using System;
namespace LINQAggregateOperatorsOne
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the smallest number in the array
             int[] Numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
             int? result = null;
             foreach(int number in Numbers)
             {
                     if(!result.HasValue || number < result)
                     {
                         result = number;
                     }
                 }
             }
             Console.WriteLine(result);
      }
}

If you wanted to find the smallest number in the array above, there are lots of routes to explore but i chose to use this route. I had to write more logic to come about the smallest number in the array.

But with LINQ, i just have to invoke the built-in methods in System.Linq namespace to perform the above operation. Let's see how this is carried out.

using System;
using System.Linq;
namespace LINQAggregateOperatorsOne
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the smallest number in the array
                int[] Numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};    
                int result = Numbers.Min();
                 Console.WriteLine(result);
        }
    }
}

I just had to write three lines of code to achieve this, See! that's how simple it is to get started using LINQ.

NB: Ensure to add the System.Linq namespace.

Let's see other examples, Finding the largest even number in our collection, let's try that without LINQ.

using System;
namespace LINQAggregateOperatorsOne
{
    class Program
    {
        static void Main(string[] args)
        {
          int[] Numbers = {1, 2, 3, 4, 5, 6. 7. 8. 9, 10};
          int? result = null
          foreach(int number in Numbers)
         {
          if(number % 2 == 0)
          {
            if(!result.HasValue || number > result)
            {
                result = number;
            }
        }
    }
}

Having to write this length of logic for just a simple operation like the largest even number in a collection could be an overkill sometimes.
Lets see how easy it is doing the above using LINQ.

using System;
using System.Linq;

namespace LINQAggregateOperatorsOne
{
    class Program
    {
        static void Main(string[] args)
        {
          int[] Numbers = {1, 2, 3, 4, 5, 6. 7. 8. 9, 10};
          int result = Numbers.Where(m => m % 2 == 0).Max();
          Console.WriteLine(result);
       }
     }
}

How better does it get? LINQ is a really powerful language construct in C# and if you are a beginner C# developer do take advantage of LINQ to write cleaner and more optimized code.

Other LINQ Aggregate methods you can explore include, Count, Sum, Aggregate, FirstOrDefault, LastOrDefault, OrderByDescending, OrderByAscending, All, Any, Append and more.

Some of these methods can be combined together like we saw in an example above.