LINQ ForEach Statement

I have a structure:

struct Student { string Name; int ClassesMissedToday; } 

Now I have a list for struct:

 List<Student> Students = new List<Student>(); 

How can I say the following in LINQ:

 Students.Add(new Student { Name = "Bob", ClassesMissedToday = 2 }) Students.Add(new Student { Name = "Bob", ClassesMissedToday = 0 }) Students.Add(new Student { Name = "Joe", ClassesMissedToday = 0 }) Students.Add(new Student { Name = "Bob", ClassesMissedToday = 1 }) 

(pseudo code)

 foreach Student.Name in Students: Console.WriteLine("Total Classes Missed Total: {0}", ClassedMissedToday(count all of them) 

I know this is pretty trivial for some, but for some reason I cannot find any valid example.

Ideas?

+4
source share
5 answers

Sum() for reference:

 int total = students.Sum(s => s.ClassesMissedToday); 

You may also find more useful collection initialization syntax since C # 3.0

 var students = new List<Student> { new Student { Name = "Bob", ClassesMissedToday = 2 }, new Student { Name = "Bob", ClassesMissedToday = 0 }, new Student { Name = "Joe", ClassesMissedToday = 0 }, new Student { Name = "Bob", ClassesMissedToday = 1 } }; 

This will do the same, as it will call the Add() method for each collection initialization base record.

+9
source

To get the total number of missing classes for all students, you can use the Sum statement.

 var totalMissed = students.Sum(s => s.ClassesMissedToday); Console.WriteLine(String.Format("Total classes missed for all students is {0}", totalMissed); 

To get the number of skipped classes when grouping by student name, you can use GroupBy and Sum operations along with an anonymous type.

 var totals = students.GroupBy(s => s.Name).Select(g => new { Name = g.Key, TotalMissed = g.Sum(s => s.ClassesMissedToday) }); foreach (var u in totals) { Console.WriteLine(String.Format("Total classes missed today for {0} is {1}", u.Name, u.TotalMissed)); } 
+4
source

Your question suggests that this is the right place to use the ForEach statement. Do you have a foreach loop in your question

 foreach Student.Name in Students: Console.WriteLine("Total Classes Missed Total: {0}", ClassedMissedToday(count all of them) 

but do you really want to write a line for the console for each student? It seems you just want to

 Console.WriteLine("Total Classes Missed Total: {0}", ClassedMissedToday(count all of them) 

and you ask: "How can I summarize the missing classes?".

You can do this with a number of LINQ statements, including the ForEach statement (as in Will Marcouiller's answer), but you want to do this using the right tool. The right tool here is the Sum operator.

 Console.WriteLine("Total Classes Missed Total: {0}", ClassedMissedToday(Students.Sum(s => s.ClassesMissedToday)) 
+2
source
 int missed; Students.ForEach(s => missed += s.ClassedMissedToday); Console.WriteLine("Total : {0}", missed); 
+1
source

Your question seems strange. Are you sure you want to simply summarize the total missed days of all students? Looking at your pseudo-code, it seems you want to write that students missed days. If so...

If you group the student name, you will only go through each name.

This is out of my head so it could contain a typo. It is also not very ...

 Students.GroupBy(student => student.Name) .Select(filtered => new object { Name = filtered.Name, Missed = filtered.Sum(groups => groups.ClassesMissedToday) }) .ToList() .ForEach(s => Console.WriteLine(string.Format("{0} has missed {1} classes", s.Name, s.Missed))); 

Is this what you are trying to accomplish?

+1
source

Source: https://habr.com/ru/post/1402993/


All Articles