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.