[LINQ] Element 로 어떤 동작 실행하는 경우 예제

Posted by RAY.D
2015. 4. 28. 13:31 Language/C#
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.




Performing Operations on Source Elements

An output sequence might not contain any elements or element properties from the source sequence. The output might instead be a sequence of values that is computed by using the source elements as input arguments. The following simple query, when it is executed, outputs a sequence of strings whose values represent a calculation based on the source sequence of elements of type double.

Note Note

Calling methods in query expressions is not supported if the query will be translated into some other domain. 

For example, you cannot call an ordinary C# method in LINQ to SQL because SQL Server has no context for it. 

However, you can map stored procedures to methods and call those. 

For more information, see Stored Procedures [LINQ to SQL].


class FormatQuery
{
    static void Main()
    {            
        // Data source. 
        double[] radii = { 1, 2, 3 };

        // Query.
        IEnumerable<string> query =
            from rad in radii
            select String.Format("Area = {0}", (rad * rad) * 3.14);

        // Query execution.  
        foreach (string s in query)
            Console.WriteLine(s);

        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    Area = 3.14
    Area = 12.56
    Area = 28.26
*/