LINQ in C# - Examples - Where - Restriction

Where - Type 1 - Example

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

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

//Output
Adams
Arthur

Where - Type 2 with index - Example

string[] presidents = { "Adams", // index 0
                                    "Arthur",  // index 1
                                    "Buchanan",// index 2
                                    "Bush",};  // index 3

 IEnumerable<string> sequence = presidents.Where((p,i) => i>2); //i is the index of the element

            foreach (var item in sequence)
                Console.WriteLine(item);
//Output
Bush

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