2015-11-04 64 views
0

我遇到了我应该使用的LINQ查询问题。使用LINQ查询对数组进行排序

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace LinqIntegersDemo.cs 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] nums = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
      // var sorted = from n in nums orderby n ascending select n; 
      int x = 0; 

      foreach (var n in nums) 
      { 
       Console.Write("Enter an integer >> "); 
       nums[x] = Convert.ToInt32(Console.ReadLine()); 
       ++x; 
      } 
      var sorted = from n in nums orderby n ascending select n; 

      foreach (var n in nums) 
      { 
       Console.WriteLine(n); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

我已经浏览了MSDN和我在那里看到的代码片段告诉我,我写了我的查询正确。那么为什么数组不按升序排序,这是我需要发生的事情。

+4

二的foreach没有使用可变的 “排序” – Measuring

+2

你想的foreach(在排序的变种N) – CooncilWorker

回答

12

因为你迭代的nums代替sorted

foreach (var n in sorted) 
{ 
    Console.WriteLine(n); 
} 
+0

@Evan你应该接受这个答案 –

+1

@ HakamFostok哈哈。 –

+3

这让我质疑我作为程序员的能力...... -.- –