Create query and where by object attribute in CSharp

Description

The following code shows how to create query and where by object attribute.

Example


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

using System.Collections;

    class Car
    {
        public string PetName;
        public string Color;
        public int Speed;
        public string Make;
    }

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList myCars = new ArrayList();  
            myCars.Add(new Car{ PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"});
            myCars.Add(new Car{ PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"});
            myCars.Add(new Car { PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford" });

            IEnumerable<Car> myCarsEnum = myCars.OfType<Car>();
            var fastCars = from c in myCarsEnum 
                           where c.Speed > 55 
                           select c;

            foreach (var car in fastCars)
            {
                Console.WriteLine("{0} is going too fast!", car.PetName);
            }
        }
    }

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ