• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

courses.ForEach(s => context.Courses.Add(s));

Does this line of code courses.ForEach(s => context.Courses.Add(s));
 is the same as ???

foreach(var s in Context.Courses){

 var courses = new List<Course>
            new Course{CourseID=1050,Title="Chemistry",Credits=3,},
            new Course{CourseID=4022,Title="Microeconomics",Credits=3,},
            new Course{CourseID=4041,Title="Macroeconomics",Credits=3,},
            new Course{CourseID=1045,Title="Calculus",Credits=4,},
            new Course{CourseID=3141,Title="Trigonometry",Credits=4,},
            new Course{CourseID=2021,Title="Composition",Credits=3,},
            new Course{CourseID=2042,Title="Literature",Credits=4,}
            courses.ForEach(s => context.Courses.Add(s));
1 Solution
Changing your foreach-statement to the following will make them similar:

foreach(var s in courses){  // <- Changed
  context.Courses.Add(s);  // <- Added

The original foreach-statement does not seem to add data to context.Courses at all. It will only perform the SaveChanges-command repeatedly...

Regards Sverre
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now