Let's start with a very simple example of a LINQ query that queries a number less than 5 in an int array and arranges it in order of size:
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 by n select N;
foreach (var n in m)
{
Console.WriteLine (n);
}
Console.ReadLine ();
}
The above code, in addition to the LINQ query syntax, is the syntax that we are familiar with, and the LINQ query syntax is known to the SQL query syntax, except in the order of precedence. Q: Why does the LINQ query syntax start with the FROM keyword, not with the SELECT keyword? The start of the select is similar to the writing of SQL, which is easier to understand?
A: In short, for the IDE's IntelliSense (intelisence) feature, the SELECT keyword is left behind.
Programming languages Write LINQ query syntax with SELECT First if you have used the VB9 CTP version for 2005 years, then VB9 's LINQ query syntax is the SELECT keyword in front, but the SELECT keyword is in front, It's a big intelisence when it comes to IntelliSense. After weighing the Microsoft IDE Group, it was determined to put the FROM keyword at the front.
For example: You see http://blog.joycode.com/saucer/archive/2005/09/16/63513.aspx this blog, at that time VB9 LINQ query syntax or select parameters at the front. Later, however, the VB9 beta was changed to the same approach as C #, with the FROM keyword in the front.
A more detailed explanation from the Assembly head
Let's say you want to write code like: Select p.name, p.age from p in persons Where XXX, the code is one character input.
The type of P is not speculative until we write to p in persons, so when we write Select p, attributes like name do not eject the smart prompt. So you need to write from this, and then come back to write Select. The Microsoft IDE Group has been thinking over and over again, so it's better to write the Select back. So the writing in the programming language is sure to write it. This change in VB9 can be reviewed in this blog: Select/from vs. From/select revisited ... Let's look at a slightly more complex LINQ query: In our list of language strings, we want to list the characters by their length, and 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
by item
Group item by item. Length into Lengthgroups
by 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 a generator for subsequent queries, which is used with group by.
Group by in LINQ is not confused with group by in SQL, because SQL is a two-dimensional structure, and some of the logic of group by is constrained by a two-dimensional structure and cannot be as flexible as group by in LINQ.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.