LINQ in C# - Examples - SelectMany(Cross Join) - Projection

  • Using Select, we can create Single output projection sequence of one Type from over an input sequence.
  • While using SelectMany we can One-Many output projection sequence from over an input sequence.
  • Although the Select operator will return one output element for every input element, SelectMany will return zero or more output elements for every input element.
  • Below example will give you an details information on Difference between Select and SelectMany 
 class Course
    {
        public int courseId;
        public string courseName;
        public List<string> students;


        static List<Course> GetCourses()
        {
            List<Course> courses = new List<Course> {
        new Course {
            courseId = 1,
            courseName = "Course 1",
            students =  new List<string> { "Student 1", "Student 2", "Student 3" }
        },
        new Course {
            courseId = 2,
            courseName = "Course 2",
            students = new List<string> { "Student A", "Student B", "Student C" }
        },
        new Course {
            courseId = 3,
            courseName = "Course 3",
            students = new List<string> { "Student X", "Student Y", "Student Z" }
        }
    };
            return courses;
        }
    }

    class Program
    {      
        static void Main(string[] args)
        {

            List<Course> Courses = new List<Course> {
        new Course {
            courseId = 1,
            courseName = "Course 1",
            students =  new List<string> { "Student 1", "Student 2", "Student 3" }
        },
        new Course {
            courseId = 2,
            courseName = "Course 2",
            students = new List<string> { "Student A", "Student B", "Student C" }
        },
        new Course {
            courseId = 3,
            courseName = "Course 3",
            students = new List<string> { "Student X", "Student Y", "Student Z" }
        }
        };

            Console.ReadLine();
        }
  • In the above given example, our object is to retrieve all the student of all who are registered in  Course 1 and Course 2
  • If we try to achieve it using Select, Refer example below
 //Students registered in Course 1 and Cource 2 using Select      
List<List<string>> Students = List<List<string>> Students = Courses.Where(x=> x.courseId==1 || x.courseId==2).Select(x => x.students).ToList();
            foreach (List<String> item in Students)
            {
                foreach (string name in item)
                {
                    Console.WriteLine(name);
                }
            }

//Output
Student 1
Student 2
Student 3
Student A
Student B
Student C
  • SelectMany - Type 1 - Example -1

  • If we try to achieve it using SelectMany, Refer example below
//Students registered in Course 1 and Cource 2 using SelectMany
            List<string> Students = Courses.Where(x=> x.courseId==1 || x.courseId==2).SelectMany(x => x.students).ToList();
            foreach (string name in Students)
            {
                Console.WriteLine(name);
            }

//Output
Student 1
Student 2
Student 3
Student A
Student B
Student C
  • While using SelectMany, able to get the same output with the less number of lines of code.
  • SelectMany as Cross join- Type 1 - Example -1 

            int[] xaxis = Enumerable.Range(1, 2).ToArray(); // 2 Element
            int[] yaxis = Enumerable.Range(10, 3).ToArray(); // 3 Element

            //So the join between these two sources can produce 2x3=> 6 combinations

            var Combinations = xaxis.SelectMany(x => yaxis.Select(y => new { X= x, Y=y }));

            foreach (var item in Combinations)
            {
                Console.WriteLine("{0}, {1}", item.X, item.Y);
            }

            Console.ReadLine();

//Output
1, 10
1, 11
1, 12
2, 10
2, 11
2, 12
  • SelectMany- with index - Type 2 - Example -1 

//Get Students registered based on Index of Cource
            List<string> Students = Courses.SelectMany((x,i) =&gt; i&gt;1? x.students: new List<string>()).ToList();
            foreach (string name in Students)
            {
                Console.WriteLine(name);
            }
//Output
Student X
Student Y
Student Z

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...