Showing posts with label How to use Foreach Loop in LINQ. Show all posts
Showing posts with label How to use Foreach Loop in LINQ. Show all posts

Thursday, December 28, 2017

How to use Foreach Loop in LINQ

public class Student
     {
            public int StudentID { get; set; }
            public string StudentName { get; set; }
        }

        public void GetStudent()
        {
            List<Student> obj = new List<Student>();

            Student objStudent1 = new Student();
            objStudent1.StudentID = 1;
            objStudent1.StudentName = "Test 1";
            obj.Add(objStudent1);

            Student objStudent2 = new Student();
            objStudent2.StudentID = 2;
            objStudent2.StudentName = "Test 1";
            obj.Add(objStudent2);

            obj.ForEach(c => {
                if (c.StudentID == 1)
                {
                    Console.Write("Student :" + c.StudentName);
                }
            });
        }