LINQ Cast Operator Syntax

By Corneliu Dinca
ODBC Drivers for QuickBooks, Salesforce, SAP, MSCRM, SharePoint … Free Trial!

The Cast<TResult>(IEnumerable) method enables the standard query operators to be invoked on non-generic collections. For example, if we want to query an ArrayList, we have to cast it first to IEnumerable<T>

           System.Collections.ArrayList array = new System.Collections.ArrayList { 1, 2, 3, 4 };
          
IEnumerable<int> evenNumbers = array.Cast<int>().Where(r => r % 2 == 0);
             evenNumbers.
ToList().ForEach
                (
                    (i) =>
                    {
                      
Console.Write(i+" ");
                     }
                 );

LINQ Cast Operator Syntax  (1596 Views)