1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

LINQ Group Operator

Discussion in 'C#' started by JCStorey, Aug 28, 2010.

  1. JCStorey

    JCStorey New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have an exercise to write a console program where the user enters 8 integers and then the integers are displayed in two groups, odd and even. I have to have an array for the 8 integers use LINQ statements to group the integers by odd and even. I have my array, but I am having trouble with the LINQ statement. My code so far is below. Any help would be appreciated.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    public class LinqIntegersDemo
    {
        public static void Main(string[] args)
        {
            int[] integer = new int[8];
            int x;
            
            for(x = 0; x < integer.Length; ++x)
            {
                Console.Write("Enter any number: ");
                integer[x] = Convert.ToInt32(Console.ReadLine());
            }
            var oddNums =
                from n in integer
                group n
                by n;
            Console.WriteLine("Integers in order: ");
            foreach (var n in oddNums)
                Console.WriteLine("{0} ", n);
            Console.WriteLine();
            System.Console.ReadLine();
        }
    }
     

Share This Page