Thứ Tư, 11 tháng 2, 2009

Làm thế nào return một truy vấn từ một Method

Ví dụ này cho thấy làm thế nào để trả về một truy vấn từ một method như là giá trị trả về và như là một tham số ra.

Bất kỳ truy vấn phải có một loại IEnumerable hoặc IEnumerable, hoặc một loại có nguồn gốc như IQueryable. Vì vậy bất kỳ giá trị trả lại hoặc tham số trong một phương thức trả về một truy vấn cũng phải có kiểu. Nếu một phương thức materializes một truy vấn vào một List hoặc Array, nó được coi là trả lại kết quả truy vấn thay vì truy vấn chính nó. Một biến truy vấn đó được trả về từ method vẫn có thể được cấu tạo hoặc sửa đổi.

Trong ví dụ sau đây, Method đầu tiên trả về câu truy vấn như là một giá trị, và Method thứ hai, một truy vấn như là một tham số ra. Lưu ý rằng trong cả hai trường hợp, nó là một truy vấn được trả về, không kết quả truy vấn.

class MQ
{
       // QueryMethhod1 returns a query as its value.
       IEnumerable 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 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 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();
      }
}

DangTrung.

Không có nhận xét nào:

Đăng nhận xét