LINQ in C# - Examples - Take and TakeWhile - Skip and SkipWhile - Partitioning

  • The partitioning operators allow you to return an output sequence that is a subset of an input sequence.
  • Take - Example -1

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", "Student 4", "Student 5" }
        },
        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" }
        }
        };

            //Take First 2 courses
            var First2CourseDetails = Courses.Take(2).Select(x=> new { Name=x.courseName, StuCount= x.students.Count() }).ToList();
            foreach (var item in First2CourseDetails)
            {
                Console.WriteLine("{0} have {1} students", item.Name, item.StuCount);
            }

            Console.ReadLine();
        }
    }

//Output
Course 1 have 5 students
Course 2 have 3 students
  • Take - Example -2

             //Take First 2 courses
            var First2CourseDetails = Courses.Take(2);
            foreach (var item in First2CourseDetails)
            {
                Console.WriteLine("{0} => {1}", item.courseId, item.courseName);
            }

            Console.ReadLine();
//Output
1 => Course 1
2 => Course 2
  • TakeWhile

  • The TakeWhile operator yields elements from an input sequence while some condition is true, starting from the beginning of the sequence. The remaining input elements will be skipped.
  • Difference between Where and TakeWhile
  • TakeWhile stops when the condition is false, Where continues and find all elements matching the condition 
  • TakeWhile - Type 1- Example -1

string[] presidents = { "Adams",
                                    "Arthur",
                                    "Buchanan",
                                    "Bush",};
            IEnumerable<string> sequence = presidents.TakeWhile(p => p.StartsWith("A"));

            foreach (var item in sequence)
                Console.WriteLine(item);

            Console.ReadLine();
//Output
Adams
Arthur
  • TakeWhile - with index Type 2- Example -2

string[] presidents = { "Adams",
                                    "Arthur",
                                    "Buchanan",
                                    "Bush",};
            IEnumerable<string> sequence = presidents.TakeWhile((p, i) => i<3); // index less than 3

            foreach (var item in sequence)
                Console.WriteLine(item);

            Console.ReadLine();
//Output
Adams
Arthur
Buchanan
  • Skip and SkipWhile works just opposite of Take and Taskwhile

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...