Method Syntax in LINQ (C#)



Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition. You also must use a method call for a query that retrieves the element that has the maximum value in a source sequence

using System;
using System.Linq;

namespace CSharpLINQ
{
    class SimpleQuery
    {
        static void Main()
        {
            int[] numbers = { 1, -2, 3, 0, -4, 5 };

            // Create a query that obtains only positive numbers.
            var posNums = numbers.Where(n => n > 0).Select(r => r);
			
            Console.Write("The positive values in numbers: ");

            // Execute the query and display the results.
            foreach (var i in posNums) Console.Write(i + " ");
        }
    }
}

0 Comment's

Comment Form