Filter and Select from Multiple Data Sources - CSharp LINQ

CSharp examples for LINQ:where

Description

Filter and Select from Multiple Data Sources

Demo Code


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

class MainClass
    {
        static void Main(string[] args)
        {
            IList<ItemColor> colorsource = createColorData();
            IList<ItemLevel> shelflifesource = createLevelData();

            var result = from e in colorsource
                         join f in shelflifesource on e.Name equals f.Name
                         where e.Color == "green"
                         select new
                         {
                             e.Name,
                             e.Color,
                             f.Level
                         };

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

            // Wait to continue.
            Console.WriteLine("\nMain method complete. Press Enter");
            Console.ReadLine();
        }

        static IList<ItemColor> createColorData()
        {
            return new List<ItemColor>()
            {

                new ItemColor("Oracle", "green"),
                new ItemColor("PLSQL", "red")
            };
        }

        static IList<ItemLevel> createLevelData()
        {
            return new List<ItemLevel>()
            {
                new ItemLevel("Oracle", 7),
                new ItemLevel("MySQL", 10),
                new ItemLevel("C", 4),
                new ItemLevel("fig", 12),
                new ItemLevel("XML", 2),
                new ItemLevel("file", 10),
                new ItemLevel("PLSQL",  7)
            };
        }
    }

    class ItemColor
    {
        public ItemColor(string namearg, string colorarg)
        {
            Name = namearg;
            Color = colorarg;
        }
        public string Name { get; set; }
        public string Color { get; set; }
    }

    class ItemLevel
    {
        public ItemLevel(string namearg, int lifearg)
        {
            Name = namearg;
            Level = lifearg;
        }
        public string Name { get; set; }
        public int Level{ get; set; }
    }

Result


Related Tutorials