Sort Query Results - CSharp LINQ

CSharp examples for LINQ:order by

Description

Sort Query Results

Demo Code


using System;/*from   ww w  . ja  va  2 s  .  c o m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MainClass
    {
        static void Main(string[] args)
        {
            // create the data source
            IList<Item> datasource = createData();

            IEnumerable<Item> result = from e in datasource
                                        orderby e.Name
                                        orderby e.Color descending
                                        select e;

            foreach (Item fruit in result)
            {
                Console.WriteLine("Name: {0} Color: {1} Shelf Level: {2} days.",
                    fruit.Name, fruit.Color, fruit.Level);
            }
        }

        static IList<Item> createData()
        {
            return new List<Item>()
            {
                new Item("Oracle", "red", 7),
                new Item("file", "yellow", 10),
                new Item("PLSQL", "red", 7)
            };
        }
    }
    class Item
    {
        public Item(string namearg, string colorarg, int lifearg)
        {
            Name = namearg;
            Color = colorarg;
            Level = lifearg;
        }
        public string Name { get; set; }
        public string Color { get; set; }
        public int Level { get; set; }
    }

Result


Related Tutorials