Aracılığıyla paylaş


Nasıl yapılır: sorgu yöntemi (C# Programlama Kılavuzu) dönmek

Bu örnek olarak, dönüş değeri olarak bir sorgu bir yöntemden almak nasıl gösterir bir out parametresi.

Herhangi bir sorgu türü olmalıdır IEnumerable veya IEnumerable<T>, veya türetilmiş bir tür gibi IQueryable<T>.Bu nedenle herhangi bir değer döndürür veya out bir sorgu döndüren bir yöntem parametresi türü de olmalıdır.Bir yöntemin bir beton bir sorgu gerçeğe, List<T> veya Array türü kabul sorgu yerine sorgu sonuçları döndüren için.Bir yöntemi tarafından döndürülen sorgu değişken hala oluşan veya değiştirilemez.

Örnek

Aşağıdaki örnekte, ilk yöntem dönüş değeri olarak bir sorgu verir ve sorgu olarak ikinci yöntemi döndürür bir out parametresi.Her iki durumda da, bir sorgu, sorgu sonuçları döndürülür olduğunu unutmayın.

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();
    }
} 

Kod Derleniyor

  • Oluşturma bir Visual Studio hedefleyen bir proje.net Framework sürüm 3.5 veya sonraki bir sürümü.Varsayılan olarak, proje başvuru System.Core.dll sahiptir ve bir using System.Linq ad alanı için yönerge.

  • Sınıf örnek kodu ile değiştirin.

  • Derlemek ve program çalıştırmak için F5 tuşuna basın.

  • Konsol penceresine çıkmak için herhangi bir tuşa basın.

Ayrıca bkz.

Kavramlar

LINQ sorgu ifadelerini (C# Programlama Kılavuzu)