0

I am having trouble with the LINQ query I am supposed to be using.

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

I have looked through MSDN and the snippets I have seen there told me that I wrote my query correctly. So why is it that the array is not being sorted in ascending order, which is what I need to happen.

2
  • 4
    Second foreach isn't using variable "sorted" Commented Nov 4, 2015 at 15:31
  • 2
    You want foreach (var n in sorted) Commented Nov 4, 2015 at 15:32

1 Answer 1

14

Because you're iterating nums instead of sorted:

foreach (var n in sorted)
{
    Console.WriteLine(n);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This makes question my ability as a programmer... -.-

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.