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:
- using System;
- namespace ConsoleAppLinqExp {
- class Program {
- static void Main(string[] args) {
- Student student = new Student();
- Console.WriteLine(GetAttribute("StudentId"));
- Console.ReadKey();
- }
- static string GetAttribute(string name) {
- return typeof(Student).GetProperty(name).Name;
- }
- }
- public class Student {
- public int StudentId {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- public string Phone {
- get;
- set;
- }
- }
- }
Running this application, we get the below,
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,
- using System;
- using System.Linq.Expressions;
- using System.Reflection;
- namespace ConsoleAppLinqExp {
- class Program {
- static void Main(string[] args) {
- Student student = new Student();
- Console.WriteLine(GetAttribute("StudentId"));
- Console.WriteLine(getPropertyName(() => student.StudentId));
- Console.ReadKey();
- }
- static string GetAttribute(string name) {
- return typeof(Student).GetProperty(name).Name;
- }
- static string getPropertyName < T > (Expression < Func < T >> lambdaExp) {
- MemberExpression me = lambdaExp.Body as MemberExpression;
- MemberInfo minfo = me.Member;
- return minfo.Name;
- }
- }
- public class Student {
- public int StudentId {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- public string Phone {
- get;
- set;
- }
- }
- }
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.
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!