Writing notes from the perspective

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.