If you are an experienceg .NET developer then you might be aware of LINQ and it's advantages. Yes it's a uniform query language to get and filter data from various data sources.
Fine, but LINQ is beyond that and we can combine various functions and operators associated with a LINQ query. Believe it or not, by using those functions we can make LINQ more robust for data processing and manipulation queries.
This article shows various functions and operators associated with LINQ and we will see how it makes data processing operations simple.
We can categorize the operators in many categories. Let's see a few of them.
Aggregation
Here we will understand various LINQ operators that will work on data, let's start with aggregation. Here is a few examples.
- namespace ConsoleApp
- {
- public class Program
- {
- public static void Main(string[] args)
- {
-
-
- int[] data = { 1, 2, 3, 4, 5 };
-
- Console.WriteLine(data.Sum());
-
- Console.WriteLine(data.Average());
-
- Console.WriteLine(data.Max());
-
- String[] strdata = {"1","two","three","four","five"};
-
-
- Console.WriteLine(strdata.Min());
-
-
- Console.WriteLine(strdata.Min(f=>f.Length));
-
-
- Console.WriteLine(strdata.Max(f => f.Length));
-
- Console.ReadLine();
- }
- }
- }
Concatination
A Concatenation operation takes place when we want to combine more than one element. In this example we have used the Concat function to combine two arrays. I believe many developers don't know the shortcut to combine two arrays, sometimes they use foreach or some kind of iterative statement to do the same job.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public static void Main(string[] args)
- {
-
- int[] data = { 1, 2, 3 };
-
-
- int[] result = data.Concat(new int[] { 4, 5, 6 }).ToArray();
-
- }
- }
Conversion
The dictionary meaning of conversion is applicable here. To convert one type to another type, we can use a Cast parameterized function. In this example I am showing how to convert from IEnumerable to a list and an array.
- public static void Main(string[] args)
- {
- object[] data = { "1", "2", "3", "4", "5" };
-
- IEnumerable<string> strEnum = data.Cast<string>();
-
- List<string> strList = data.Cast<string>().ToList();
-
- string[] strArray = data.Cast<string>().ToArray();
- }
Element operation
It's very common in applications that we sometimes need a direct element, direct in the sense that we want to pick the element by it's index or by a query. There are many functions available with which we can combine a lambda expression to simplify our job.
- public static void Main(string[] args)
- {
- int[] data = { 1, 2, 3, 4, 5 };
- data.ElementAt(10);
- data.ElementAtOrDefault(10);
- data.First();
- string[] name = {"sourav","Ram","Shyam"};
-
- name.First(m => m.Length == 3);
-
- name.First(m => m.Length > 3);
-
- name.Last();
-
- name.Single();
-
- name.SingleOrDefault();
-
- name.SingleOrDefault(m => m.Length == 10);
- }
Equality
Equality checking between lists or some kind of collection is another common requirement in applications. If we want to check whether or not the words are sequentially arranged then we can use the SequenceEqual function. Although there are many I will mention the uncommon one.
- public static void Main(string[] args)
- {
-
- string[] words = { "sourav", "Ram", "Shyam" };
-
-
- words.SequenceEqual(new[] { "sourav", "Ram", "Shyam" });
-
-
- words.SequenceEqual(new[] { "SOURAV", "RAM", "SHYAM" });
-
-
- words.SequenceEqual(new[] { "SOURAV", "RAM", "SHYAM"}, StringComparer.OrdinalIgnoreCase);
- }
Partitioning
This operation divides the entire set into two partitions and picks one of them. In this example we will see few functions combine with a lambda expression.
- public static void Main(string[] args)
- {
- string[] name = { "one","two", "three" };
-
- name.Take(2).ToList<string>();
-
-
- name.Skip(1).ToList<string>();
-
-
- name.TakeWhile(f => f.Length == 3);
-
-
- name.SkipWhile(f => f.Length < 4);
-
- Console.ReadLine();
- }
Projection
This operation is needed when we want to take a certain part from a collection. Here, we are taking part of a name array by length, by its index position. Have a look at the following example.
- public static void Main(string[] args)
- {
- string[] name = { "one","two", "three" };
-
- name.Select(f => f.Length);
-
- name.Select((a, b) => b.ToString() + ":" + a);
-
-
-
-
- name.SelectMany((a, b) => Enumerable.Repeat(a, b)).ToArray();
- Console.ReadLine();
- }
Set based operation
A set based operation is not a very common in day-to-day programming, but it's useful to understand an overview of the functions. It's very similar to a normal math set operation. Just have a quick look at the following example.
- public static void Main(string[] args)
- {
-
- string[] first = { "a", "b", "b" ,"c" };
- string[] second = { "c", "d", "e" };
-
-
- first.Distinct();
-
-
- first.Intersect(second);
-
-
- first.Union(second);
-
-
- first.Except(second);
- Console.ReadLine();
- }
Filtering
Filtering is very essential in day-to-day programming. In this example we will see a few useful filtering functions associated with lambda expressions.
- public static void Main(string[] args)
- {
-
- string[] name = {"one","two","three" };
-
-
- name.OrderBy(f => f).ToArray();
-
-
- name.OrderBy(f => f[1]);
-
-
- name.OrderBy(f => f.Length);
-
-
- name.OrderByDescending(f => f.Length);
-
-
- name.OrderBy(f => f.Length).ThenByDescending(f => f);
-
- Console.ReadLine();
- }
Conclusion
This article showed various functions associated with LINQ and lambda expressions. It's good to understand those functions to reduce the amount of code and program complexity.