次の方法で共有


How to: Return a Query from a Method (C# Programming Guide)

This example shows how to return a query from a method as the return value and as an out parameter.

Any query must have a type of IEnumerable or IEnumerable<T>, or a derived type such as IQueryable<T>. Therefore any return value or out parameter of a method that returns a query must also have that type. If a method materializes a query into a concrete List<T> or Array type, it is considered to be returning the query results instead of the query itself. A query variable that is returned from a method can still be composed or modified.

Example

In the following example, the first method returns a query as a return value, and the second method returns a query as an out parameter. Note that in both cases it is a query that is returned, not query results.

class MQ
{
    // QueryMethhod1 returns a query as its value.
    IEnumerable<string> QueryMethod1(ref int[] ints)
    {
        var intsToStrings = from i in ints
                            where i > 4
                            select i.ToString();
        return intsToStrings;
    }

    // QueryMethod2 returns a query as the value of parameter returnQ. 
    void QueryMethod2(ref int[] ints, out IEnumerable<string> returnQ)
    {
        var intsToStrings = from i in ints
                            where i < 4
                            select i.ToString();
        returnQ = intsToStrings;
    }

    static void Main()
    {
        MQ app = new MQ();

        int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        // QueryMethod1 returns a query as the value of the method. 
        var myQuery1 = app.QueryMethod1(ref nums);

        // Query myQuery1 is executed in the following foreach loop.
        Console.WriteLine("Results of executing myQuery1:");
        // Rest the mouse pointer over myQuery1 to see its type. 
        foreach (string s in myQuery1)
        {
            Console.WriteLine(s);
        }

        // You also can execute the query returned from QueryMethod1  
        // directly, without using myQuery1.
        Console.WriteLine("\nResults of executing myQuery1 directly:");
        // Rest the mouse pointer over the call to QueryMethod1 to see its 
        // return type. 
        foreach (string s in app.QueryMethod1(ref nums))
        {
            Console.WriteLine(s);
        }


        IEnumerable<string> myQuery2;
        // QueryMethod2 returns a query as the value of its out parameter.
        app.QueryMethod2(ref nums, out myQuery2);

        // Execute the returned query.
        Console.WriteLine("\nResults of executing myQuery2:");
        foreach (string s in myQuery2)
        {
            Console.WriteLine(s);
        }


        // You can modify a query by using query composition. A saved query 
        // is nested inside a new query definition that revises the results 
        // of the first query.
        myQuery1 = from item in myQuery1
                   orderby item descending 
                   select item;

        // Execute the modified query.
        Console.WriteLine("\nResults of executing modified myQuery1:");
        foreach (string s in myQuery1)
        {
            Console.WriteLine(s);
        }

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
} 

Compiling the Code

  • Create a Visual Studio project that targets the .NET Framework version 3.5 or a later version. By default, the project has a reference to System.Core.dll and a using directive for the System.Linq namespace.

  • Replace the class with the code in the example.

  • Press F5 to compile and run the program.

  • Press any key to exit the console window.

See Also

Concepts

LINQ Query Expressions (C# Programming Guide)