.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Select and SelectMany: LINQ projection operator

Posted By:Dhananjay Kumar       Posted Date: August 17, 2010    Points: 25    Category: LINQ    URL: http://www.dotnetspark.com  

Select 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 


1.gif

Let us say, there is a class 


2.gif 


And a function returning List as below, 


3.gif 


Select operator 


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


4.gif 


Output 


5.gif 


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


6.gif 


Output 


7.gif 


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


8.gif

Output 


9.gif



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. 


10.gif 


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



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


12.gif 


Output 


13.gif 


Full Source code is as below, 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication9
{
   class Program
   {
       static void Main(string[] args)
       {
           // Reteriving all students with name D
           var result1 = from r in GetStudents()
                         where r.Name.StartsWith("D")
                         select r;
           foreach (var r in result1)
           {
               Console.WriteLine(r.Name);
           }
           // Reteriving  the result in Anonymous  class
           var result2 = from r in GetStudents()
                         select new { r.RollNumber, r.Name };
           foreach (var r in result2)
           {
               Console.WriteLine(r);
           }
           // Reteriving using SelectMany
           var result3 = from r in GetStudents()
                         select r;
           foreach (var r in
               result3.SelectMany(Student => Student.Subject))
           {
               Console.WriteLine(r);
           }
           // directly applying  SelectMany
     var result = GetStudents().AsQueryable().SelectMany(Subject => Subject.Subject);
           foreach (var r in result)
           {
               Console.WriteLine(r);
           }
           Console.Read();
       }
       static List GetStudents()
       {
           List students = new List{
                                   new Student {
                                       Name = "Dhananjay",
                                       RollNumber ="1" ,
                                       Subject= new List{"Math","Phy"}},
                                   new Student {
                                       Name = "Scott",
   RollNumber ="2" ,
                                       Subject= new List{"Che","Phy"}},
                                   new Student {
                                       Name = "John",
           RollNumber ="3" ,
                                       Subject= new List{"Hindi","Phy"}}};
           return students;
       }
   }
   class Student
   {
       public string Name { get; set; }
       public string RollNumber { get; set; }
       public List Subject { get; set; }
   }
}

You can also download the sample project by clicking below link

I hope this article was useful. Thanks for reading. Happy Coding.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend