Select and SelectMany: LINQ projection operator

Posted By:Dhananjay KumarPosted Date:August 17, 2010Points:25
Category: LINQURL:http://www.dotnetspark.comSelect and SelectMany: LINQ projection operator. Projection transforms the query result into the form defined by the developer. There are two projection operators in LINQ

Projection transforms the query result into the form defined by the developer.
There are two projection operators in LINQ

Let
us say, there is a class

And
a function returning List as below,

Select
operator

Below query will return name and roll number of all the
students.

Output

Below
query will project name of the student's starts with D.

Output

Above
was very simple query let us modify the display function and try to display
subject of the student

Output

If
you notice the above output, we are not getting the proper output and it is
saying that Subject is generic list. So to fetch we need to enumerate through
the list.

Now question is how to retrieve all the subjects of
students?

SelectMany

So to retrieve query from
more than one collection SelectMany come into action.

Above
query will return the entire subjects of all the students.

The
other way to apply SelectMany operator is directly apply on the retrieval query
as below,