There have been too many articles about what linq is and so on, and it is unclear that Hu can search for a lot of answers by himself.
To get a better experience from the code:
String [] words = new string [] {"test", "test1", "linqtosql", "linqtoobject", "beautiful "};
Var result = from p in words
Where p. Length> 5
Select p;
Console. WriteLine (result. GetType ());
Foreach (var item in result)
{
Console. WriteLine (item );
}
Console. Read ();
The above code is used to obtain a set with a length greater than 5 from an array. The result type returned by linq can be result. getType (). Let's look back at how we can implement the same functions before we can implement the same functions. Is it necessary to do this:
Foreach (var item in words)
{
If (item. length> 5)
Console. writeline (item );
}
From the above two simple codes, it seems that linq has not brought us any amazing things, but let's continue to look at it.
String [] words = new string [] {"test", "tes1", "linqtosql", "linqtoobject", "beautiful "};
Var result = from p in words
Orderby p descending
Group p by p. Length into tempqroup
Orderby tempqroup. Key descending
Select new {
Length = tempqroup. Key,
Words = tempqroup
};
Foreach (var item in result)
{
Foreach (var p in item. words)
{
Console. WriteLine (p );
}
}
Console. Read ();
The above is a function to group the array content by word length. If we want to implement the traditional code, we will find that we have to write a lot of code, right?
Now that the end of the linq appetizer is complete, more applications will be available in the future.