filter using two characteristics with AND condition - CSharp LINQ

CSharp examples for LINQ:where

Description

filter using two characteristics with AND condition

Demo Code


using System;/*from   w ww . ja v a  2s .c  o m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

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


            // filter using two characteristics
            IEnumerable<string> result3 = from e in datasource
                                          where e.Color == "green" && e.Level > 5
                                          select e.Name;
            Console.WriteLine("\nFilter for green fruit and life > 5 days");
            foreach (string str in result3)
            {
                Console.WriteLine("Item {0}", str);
            }

        }

        static IList<Item> createData()
        {
            return new List<Item>()
            {
                new Item("Oracle", "green", 7),
                new Item("MySQL", "MySQL", 10),
                new Item("C", "green", 4),
                new Item("CSS", "brown", 12),
                new Item("C", "green", 4),
                new Item("HTML", "brown", 12),
                new Item("XML", "red", 2),
                new Item("Javascript", "green", 4),
                new Item("fig", "brown", 12),
                new Item("SQL", "red", 2),
                new Item("C", "green", 4),
                new Item("SVG", "brown", 12),
                new Item("XML", "red", 2),
                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