Coderes
Published in

Coderes

LINQ in C#- A strange charisma

LINQ in C#- A strange charisma
  1. Is LINQ really as easy as we thought?
    LINQ is not a strange concept for C# .NET developers, especially those who work with databases (LINQ to SQL). However, most of us use LINQ without knowing how it works. Probably everyone wrote the following lines of code:
What is LINQ and How it works?

The code is very easy to understand, filtering out students with the age of <20. However, can you answer the following questions:

  1. What is the sign “=>”. What is the whole cluster “stu => stu.Age < 20”?
  2. How is the Where function written, what parameters are inputted, what value is returned?
  3. Why does IDE know that stu is 1 student to be able to prompt?
  4. Can we write something like LINQ in javascript?

If you only answer 1,2 or don’t answer any questions, you don’t really understand LINQ.
Don’t worry, I was the same in the old days. This article will help you answer these questions, as well as better understand LINQ. (Answer at the end of the article).

2. Review knowledge

LINQ is implemented by summarizing quite a number of interesting and complex concepts of C # language. For those who do not know, these concepts include:

Extension method.
Callback and Delegate (Func, Predicate).
Lamda Expression.
Generic.
IEnumrable and yield.

3. Let’s unmask the true about LINQ

To use LINQ, we must use the System.Linq namespace. Actually, Linq is a list of extension methods, adding some methods for interface IENumerable. Because of List, DbSet, … classes implement this interface, they can all call the Linq method.
This is the signature of the 3 basic Linq methods: Where, Select and First.

the signature of the 3 basic Linq methods

Yes, you will be surprised: Is this ugly, ugly hunk of your beautiful Linq? Yes, once upon a time after watching my girlfriend remove her makeup, I also felt like you =)))
Within the article, I just explained the method Where, you can rely on it to learn more. I see that the method to receive an object of type
Func <TSource, bool> (link), is it a familiar one? This is a delegate, pointing to a method that has params as TSource, the return type is bool. TSource here is a generic data type, so whatever type we can declare.

method Where

4. Write LINQ by yourself
At this point, we can write 1 Linq method. It’s very simple, you just need to write 1 extension method for IENumerable, with signature similar to Linq. This is the Linq method I wrote

MyLinq class in C#

This method is quite simple because I skipped the check steps null v … v, but it gives the same result as the method Where in Linq.

At this point, I will answer 4 questions raised at the beginning of the lesson.

  1. What is the sign “=>”. What is the whole cluster “stu => stu.Age <20”?
    The “=>” sign is a “go-to” sign, the entire cluster is accompanied by a lambda expression. The nature of lambda expression is an anonymous method.
  2. How is the Where function written, what parameters are inputted, what value is returned?
    The Where function is an extension method of the IEnumerable class, get into a delegate with the return type of bool, return an IEnumerable filtered.
  3. Why does IDE know that stu is 1 student to be able to prompt?
    Here we use a lambda expression, stu is 1 param of function. Because generic Linq uses, list students is a list with Student data type. therefore C # understands that stu is a param with Student data type.
  4. Can we write something like LINQ in javascript?
    Of course. You can learn more about underscore and lodash, 2 linq support libraries for javascript.

The article ends here. Because LINQ is a quite complex topic, need a lot of explanations, if you have any questions, you can leave a comment.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beribey

Beribey

40 Followers

Always be nice to anybody who has access to my toothbrush.