Linq Reading Notes 3-select and select.pdf Operators

Source: Internet
Author: User

There are two forms of data query by using linq:

Var demo = from p in pList

Where p. id = ***

Select p;

Var demo = pList. where (p => p. id = ***) select p;

The above are two common expressions of linq recently. Linq supports operations with multiple operators. The following describes the main common operators:

Select and selectworkflow

Select operator declaration:

Public static IEnumerable <s> select <T, S> {

This IEnumerable <T> source,

Func <T, S> selector

}

The select operator evaluates each element in the source Sequence Based on the selector function and returns the value in sequence to form a sequence of returned values.

For example:

Var book = books. Select (p => p. Name );

The preceding select clause projects the sequence of the book object to the object sequence of a string. Of course, there can be more complex applications such as projection the sequence to an object, the following code is to find the publisher Object from the book collection, and then project the sequence to a specific object.

Var publisher = from p in books

Select p. publisher

In the result set returned by select, the object can be the original object or a new object that contains several fields.

For example:

Var Books = from book in sampleBookData. Books

Select new {book. title, book. price, book. publisher. Name };

It can also be written as follows:

Var Books = from book in sampleBookData. Books

Select new {

Title = book. title,

Price = book. price,

PublisherName = book. publisher. Name

};

 

This operation creates a projection of the original data, which is also an important reason for classifying such operations as projection.

Next, let's look at another projection operation: selectprojection.

Operator declaration:

Public static IEnumerable <s> select <T, S> {

This IEnumerable <T> source,

Func <T, IEnumerable <s> selector

}

Unlike select, selectsequence returns a sequence.

The difference between selectsequence and select is that it connects each element in the returned sequence set to form a new sequence.

Here we will first compare the two sections of code:

Var tmp = sampleBook. select (p => p. books. author );

Foreach (var item in tmp)

{

Foreach (var item1 in item. author)

{

Console. writeline (item1.name );

}

}

The above code is used to query the information of authors in all books. The author attribute of book objects is composed of author objects, therefore, the select Operation returns the image sequence composed of these image arrays, that is, the book and author are returned. The book contains the image sequence of author.

Next let's take a look at the selectpipeline code.

 

Var tmp = sampleBook. selectiterator (p => p. books. author );

Foreach (var item in tmp)

{

Console. writeline (item. Name );

}

It can be clearly seen that the selectmany code is much simpler. We need the author's information and do not need to return all the sequence information, selectsequences can precisely separate the two sequences and form a new sequence to return to us, which is exactly what we need.

 

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.