Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write LINQ query syntax

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to write the syntax of LINQ query for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

LINQ query syntax

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.

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? To put it simply, for the sake of IDE's IntelliSense (Intelisence) function, the select keyword is placed at the end.

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, the query syntax of VB9 LINQ still comes first with the select parameter. But then the VB9 beta was changed to do the same thing as C #, with the from keyword at the top. For a more detailed explanation, from the assembly head, suppose you want to write code like: 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.

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 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.

This is the end of this article on "how to write LINQ query Grammar". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report