Convert from IEnumerable<> - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Convert from IEnumerable<>

Demo Code


using System;/*ww w  .  j a  v  a  2  s.  com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MainClass
    {
        static void Main(string[] args)
        {
            IEnumerable<Item> datasource = createData();

            // perform a query
            IEnumerable<Item> result = from e in datasource
                                        where e.Level <= 7
                                        select e;

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

            Item[] array = result.ToArray<Item>();

            Console.WriteLine("\nResults from array");
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("Name: {0} Color: {1} Shelf Level: {2} days.",
                                    array[i].Name, array[i].Color, array[i].Level);
            }

            Dictionary<string, Item> dictionary = result.ToDictionary(e => e.Name);
 
            Console.WriteLine("\nResults from dictionary");
            foreach (KeyValuePair<string, Item> kvp in dictionary)
            {
                Console.WriteLine("Name: {0} Color: {1} Shelf Level: {2} days.",
                    kvp.Key, kvp.Value.Color, kvp.Value.Level);
            }

            IList<Item> list = result.ToList<Item>();
            Console.WriteLine("\nResults from list");
            foreach (Item fruit in list)
            {
                Console.WriteLine("Name: {0} Color: {1} Shelf Level: {2} days.",
                    fruit.Name, fruit.Color, fruit.Level);
            }

            ILookup<string, Item> lookup = result.ToLookup(e => e.Color);
            // print out the contents of the list
            Console.WriteLine("\nResults from lookup");
            IEnumerator<IGrouping<string, Item>> groups = lookup.GetEnumerator();
            while (groups.MoveNext())
            {
                IGrouping<string, Item> group = groups.Current;
                Console.WriteLine("Group for {0}", group.Key);
                foreach (Item fruit in group)
                {
                    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("MySQL", "MySQL", 10),
                new Item("C", "green", 4),
                new Item("fig", "brown", 12),
                new Item("XML", "red", 2),
                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