Part 8 SelectMany Operator in LINQ

25
25



Text version of the video

Slides

LINQ Tutorial – All Text Articles & Slides

LINQ Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

SelectMany Operator belong to Projection Operators category. It is used to project each element of a sequence to an IEnumerable[T] and flattens the resulting sequences into one sequence.

Example 1: Projects all subject strings of a given a student to an IEnumerable[string]. In this example since we have 4 students, there will be 4 IEnumerable[string] sequences, which are then flattened to form a single sequence i.e a single IEnumerable[string] sequence.

IEnumerable[string] allSubjects = Student.GetAllStudetns().SelectMany(s =] s.Subjects);
foreach (string subject in allSubjects)
{
Console.WriteLine(subject);
}

Example 2: Rewrite Example1 using SQL like syntax. When using SQL like syntax style, we don’t use SelectMany, instead we will have an additional from clause, which will get it’s data from the results of the first from clause.

IEnumerable[string] allSubjects = from student in Student.GetAllStudetns()
from subject in student.Subjects
select subject;

foreach (string subject in allSubjects)
{
Console.WriteLine(subject);
}

Example 3: Projects each string to an IEnumerable[char]. In this example since we have 2 strings, there will be 2 IEnumerable[char] sequences, which are then flattened to form a single sequence i.e a single IEnumerable[char] sequence.

string[] stringArray =
{
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”,
“0123456789”
};

IEnumerable[char] result = stringArray.SelectMany(s =] s);
foreach (char c in result)
{
Console.WriteLine(c);
}

Example 4: Rewrite Example3 using SQL like syntax.
string[] stringArray =
{
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”,
“0123456789”
};

IEnumerable[char] result = from s in stringArray
from c in s
select c;

foreach (char c in result)
{
Console.WriteLine(c);
}

Example 5: Selects only the distinct subjects
IEnumerable[string] allSubjects = Student.GetAllStudetns().SelectMany(s =] s.Subjects).Distinct();
foreach (string subject in allSubjects)
{
Console.WriteLine(subject);
}

Example 6: Rewrite Example 5 using SQL like syntax.
IEnumerable[string] allSubjects = (from student in Student.GetAllStudetns()
from subject in student.Subjects
select subject).Distinct();

foreach (string subject in allSubjects)
{
Console.WriteLine(subject);
}

Example 7: Selects student name along with all the subjects
var result = Student.GetAllStudetns().SelectMany(s =] s.Subjects, (student, subject) =]
new { StudentName = student.Name, Subject = subject });

foreach (var v in result)
{
Console.WriteLine(v.StudentName + ” – ” + v.Subject);
}

Example 8: Rewrite Example 7 using SQL like syntax.
var result = from student in Student.GetAllStudetns()
from subject in student.Subjects
select new { StudnetName = student.Name, Subject = subject };

foreach (var v in result)
{
Console.WriteLine(v.StudnetName + ” – ” + v.Subject);
}

Nguồn:https://wijstaanvooronzegrondrechten.org/

25 COMMENTS

  1. var result = Student.GetAllStudetns().SelectMany(s => s.Subjects, (student, subject) =>

    new { StudentName = student.Name, Subject = subject });

    Instead of above why can not we use like below

    var result = Student.GetAllStudetns().SelectMany(s => s.Subjects, ( subject,student) =>

    new { StudentName = student.Name, Subject = subject });
    i.e subject first then student in SelectMany
    Can someone please explain

  2. Hi, in the last example

    var result = from student in Student.GetAllStudetns()
    from subject in student.Subjects
    select new { StudnetName = student.Name, Subject = subject };

    can we use it like below

    var result = from student in Student.GetAllStudetns()
    select new { StudnetName = student.Name, Subject = student.Subjects};

    Instead of having one more from to get the subject, cant we use it directly?
    Can anybody please explain this.

  3. Hi Venkat, how does it know that it doesn't have to list the two list items such as
    "ABCDE…..Z"
    "0123…9"
    but take it apart to pieces? I would expect the above to happen… But this is a very good example to introduce inner joins, I'm expecting that to come. 🙂

  4. Does anyone else get the YouTube advertisements, about people that look like Neanderthals or drug addicts, and were able to score a cool job by learning to program on-line?  LOL

  5. Hi Venkat, can you please help me understand how variables student & subject work in example 7. How they get Type associated to them?

  6. If the SelectMany method projects all the Subjects of a given student into a IEnumerable<string> and then flattens all the results into one IEnumerable<string>, why is the Console.WriteLine method printing the subjects one by one, and not as a list of subjects for a given student?

LEAVE A REPLY

Please enter your comment!
Please enter your name here