In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Linq query syntax". In daily operation, I believe many people have doubts about how to use Linq query syntax. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Linq query syntax". Next, please follow the editor to study!
First, let's take a look at a very simple example of a Linq query that queries an int array of numbers less than 5 and arranges them in size order:
Class Program {static void Main (string [] args) {int [] arr = new int [] {8,5,89,3,56,4,1,58}; var m = from n in arr where n < 5 orderby n select n; foreach (var n in m) {Console.WriteLine (n);} Console.ReadLine ();}}
The above code is familiar to us except the Linq query syntax, while the Linq query syntax is very familiar with the SQL query syntax, except for the sequence.
Q: why does the Linq query syntax start with the from keyword instead of the select keyword? At the beginning of select, this way of writing is closer to and easier to understand with SQL?
A: to put it simply, the select keyword is placed at the back for the Intelisence function of IDE.
It is not uncommon for programming languages to write Linq query syntax at the beginning of select. If you have used the 2005 version of VB9 CTP, the Linq query syntax of VB9 is the select keyword first, but the select keyword comes first, which is very big when doing Intelligent perception (Intelisence). After the tradeoff of the Microsoft IDE group, it is determined to put the from keyword first.
For example: sometimes the VB9 Linq query syntax or the select parameter comes first. But then the VB9 beta was changed to do the same thing as C #, with the from keyword at the top.
Suppose you want to write code like this: Select p.Name, p.Age From p In persons Where xxx, the code is entered character by character.
Before we wrote about p in persons, the type of p could not be speculated, so we wrote Select p. Properties such as Name do not pop up with smart prompts.
In this way, you need to write the sentence From first, and then come back to write Select.
After much deliberation, the Microsoft IDE group decided that it would be better to write Select later. So the writing method in the programming language is determined to be written in this way.
Select/From vs. From/Select revisited...
Let's look at a slightly more complex Linq query:
In the language strings we have listed, we want to list them according to the length of the characters. The implementation code is as follows:
Static void Main (string [] args) {string [] languages = {"Java", "C#", "C++", "Delphi", "VB.net", "VC.net", "C++ Builder", "Kylix", "Perl", "Python"}; var query = from item in languages orderby item group item by item.Length into lengthGroups orderby lengthGroups.Key descending select lengthGroups Foreach (var item in query) {Console.WriteLine ("strings of length {0}", item.Key); foreach (var val in item) {Console.WriteLine (val);}} Console.ReadLine ();}
The into keyword indicates that the result of the previous query is treated as the generator of the subsequent query, which is used with group by.
Group by in Linq query should not be confused with Group by in SQL. Because SQL is a two-dimensional structure, some logic of Group by is constrained by two-dimensional structure, so it can not be as flexible as Group by in Linq query.
At this point, the study of "how to use Linq query syntax" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.