If you want to talk about the elegant grammar of which backend language, then we have to mention C#. In my opinion, LINQ grammar can be said to be an important part of its elegance!
Usually, the data queried from the backend is not directly returned to the frontend for use (or the frontend...), but requires secondary processing and screening processing and other operations! This operation is inseparable from LINQ. Otherwise, if the for loop is fitted with for loops, it is definitely feasible. However, the readability of the code is completely lost. Other colleagues will also joke with people when they see it. Even the compiler complains about this code (......), which leads to poor performance of the program!
Of course, if SQL is used to process the data in one go, it is OK, but in actual project development, few of which can be processed in one go often require secondary processing (for example: I not only need List, but also statistical information, total number, mean, sum, etc.)
Linq Lambda
Speaking of Linq, we have to mention Lambda expressions. To be honest, in actual development, there are not many related codes written directly with Linq, and Lambda expressions are everywhere!
First, let's see what the difference between them is
static void Main(string[] args) { List<User> users = new List<User>(); var letters = "abcdefghijklmnopqrstuvwxyz"; var rand = new Random(); for (int i = 0; i < 10; i++) { string name = default; for(int j = 0; j < 4; j++) { name += letters[(25)]; } (new User { Name = name, Age = (1, 99) }); } //Linq IEnumerable<User> usera = from u in users where < 35 select u; //(); //Lambda List<User> userb = (x => < 35).ToList(); // Check out the output foreach(User u in usera) { ("name:{0},age:{1} \n", , ); } ("------------------"); foreach (User u in userb) { ("name:{0},age:{1} \n", , ); } (); } class User { public string Name { get; set; } public int Age { get; set; } }
After reading the code, do you feel that Linq is similar to SQL? Yes, they are similar languages. I personally guess it should be a kind of syntax sugar imitated (it is unknown who imitates whom it is). It is better to be similar, it is more convenient to learn. And Lambda expressions are really comfortable to write!
So, what's the relationship between Linq and Lambda
The relationships contained, Linq is more wider, and the Lambda expression looks like an arrow function, that is! , it can also be said to be an anonymous function! It is equivalent to Linq's secondary encapsulated syntax sugar bar (personal understanding)
Basically, Lambda expressions can be implemented by Linq.
Usually, when it comes to Linq writing, the default is Linq Lambda expression. Direct chain writing is concise and readable, and it is also very convenient to maintain in the later stage.
As we all know, the functions that SQL can implement are very powerful, and Linq can also implement them.
However, in actual development, SQL is generally written in quite complex ways, aggregation queries are common, while Linq rarely involves (can also query multiple tables), and more of them are operations such as deduplication, filtering, sorting, grouping, statistical calculations, etc.
One-time usage experience in the Linq project
Obtain permission menus for different users. When the user logs in, he can obtain the user's permission menu collection (id collection). For the specific display menu, you also need to go to the entire menu collection to find the corresponding one, then filter it out, return it to the front end, and finally render it and display it!
If the menu collection List is all first-level menu, it is quite simple, but if there are second-level and third-level menus, it will be more troublesome. During this filtering process, I used Linq to implement some functions.
//powers User permission menu collection//menus All menu collection// Secondary menu collection (from sub in where (x=> == ) select sub).Count()
As shown in the above code segment, you can get the number of submenu of the current menu
If the outer Count() is not added, the result is the current menu submenu (this user has permissions) collection
If Linq is not used, it is difficult to implement one line of code (there are still methods, but it is not as convenient as Linq)
Linq to js [Applying on the front end]
LINQ is actually not exclusive to the backend language. The front-end can also be used. You only need to reference the corresponding js plugin.
There are corresponding plug-ins in js, vue and other projects (such as jslinq). You only need to introduce plug-ins into the corresponding components (can also be referenced on the master page or globally), and then you can use them like in the back-end language.
The specific writing method is similar to the backend language, and the common method names are the same
To talk about the difference, the only bad experience I feel is that there is no code prompt when using the front-end. If you are not familiar with LINQ, you still need to use relevant documents when using LINQ. It is naturally not a problem.
I won’t go into detail about the specific usage of Linq on the front end here. You can query it yourself if you need it. Here I will only make a brick-throwing effect.
This is the article about the detailed explanation of C# collection query Linq in the project. For more related contents of C# collection query Linq, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!