Using LINQ Expressions In C#

Introduction

 
In today’s article we will look at LINQ expressions. What are LINQ expressions? What are the advantages of using them? Finally, we will look at a code usage example of LINQ expressions and compare it to code not using them.

What are LINQ Expressions?

 
LINQ expressions are not the same as LINQ queries. We can define LINQ expressions as reflections for the C# code. If the “Type” works on a class than the “Expression” works on the code itself. This might sound confusing and the best way to understand the use of LINQ expressions is to see an example.

Using LINQ Expressions

 
We often come across a scenario where we need to pass the name of a property of a class to some function. This can be to store in a list or some key-value pair storage for configuration etc. The simple way to do this is to pass the name of the property as a string. Please see the below code which is a console application. It has been created using Visual Studio 2019 Community Edition and is a .NET Core 3.1 application:
  1. using System;  
  2. namespace ConsoleAppLinqExp {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             Student student = new Student();  
  6.             Console.WriteLine(GetAttribute("StudentId"));  
  7.             Console.ReadKey();  
  8.         }  
  9.         static string GetAttribute(string name) {  
  10.             return typeof(Student).GetProperty(name).Name;  
  11.         }  
  12.     }  
  13.     public class Student {  
  14.         public int StudentId {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         public string Name {  
  19.             get;  
  20.             set;  
  21.         }  
  22.         public int Age {  
  23.             get;  
  24.             set;  
  25.         }  
  26.         public string Phone {  
  27.             get;  
  28.             set;  
  29.         }  
  30.     }  
  31. }  
Running this application, we get the below,
 
Using LINQ Expressions in C#
 
Here, we are passing the name of a property as a string and getting details of that property using the “typeof” and “GetProperty” methods. This works fine. However, if we pass an incorrect value in the string parameter, it will still compile fine, but crash at runtime.
 
Now, we look at the use of LINQ expressions. We add another function as below,
  1. using System;  
  2. using System.Linq.Expressions;  
  3. using System.Reflection;  
  4. namespace ConsoleAppLinqExp {  
  5.     class Program {  
  6.         static void Main(string[] args) {  
  7.             Student student = new Student();  
  8.             Console.WriteLine(GetAttribute("StudentId"));  
  9.             Console.WriteLine(getPropertyName(() => student.StudentId));  
  10.             Console.ReadKey();  
  11.         }  
  12.         static string GetAttribute(string name) {  
  13.             return typeof(Student).GetProperty(name).Name;  
  14.         }  
  15.         static string getPropertyName < T > (Expression < Func < T >> lambdaExp) {  
  16.             MemberExpression me = lambdaExp.Body as MemberExpression;  
  17.             MemberInfo minfo = me.Member;  
  18.             return minfo.Name;  
  19.         }  
  20.     }  
  21.     public class Student {  
  22.         public int StudentId {  
  23.             get;  
  24.             set;  
  25.         }  
  26.         public string Name {  
  27.             get;  
  28.             set;  
  29.         }  
  30.         public int Age {  
  31.             get;  
  32.             set;  
  33.         }  
  34.         public string Phone {  
  35.             get;  
  36.             set;  
  37.         }  
  38.     }  
  39. }  
In this function, we pass a lambda as a LINQ expression and extract the property name from it. As you can see here there is no string parameter and we select the field name using a lambda. Hence, any mistake here would be a compile time error.
 
When we run the application, we get the same results but with more robust code.
 
Using LINQ Expressions in C#
 
The above is a simple example of the power of using LINQ expressions.
 

Summary

 
In this article, we looked atwhat LINQ expressions are and how we can use them to get reflection of our code.We looked at the advantage of using them as compared to passing property names as strings. Happy Coding!