Cars going faster than 55, ordered by PetName : foreach loop « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;

    class Car
    {
        public string PetName;
        public string Color;
        public int Speed;
        public string Make;
        
        public override string ToString()
        {
            return string.Format("Make={0}, Color={1}, Speed={2}, PetName={3}",Make, Color, Speed, PetName);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car[] myCars = new []{
                new Car{ PetName = "A", Color = "Silver", Speed = 100, Make = "BMW"},
                new Car{ PetName = "B", Color = "Black", Speed = 55, Make = "VW"},
                new Car{ PetName = "C", Color = "White", Speed = 43, Make = "Ford"}
            };
        
           
            var subset = from c in myCars where c.Speed > 55 orderby c.PetName descending select c;
            foreach (Car c in subset)
            {
                Console.WriteLine(c.ToString());
            }
        
        }
    }








22.14.foreach loop
22.14.1.Use foreach loop to deal with the result from linq
22.14.2.Linq Over Array Using Sequence
22.14.3.Linq Over ArrayList
22.14.4.Linq To Objects
22.14.5.Linq over array
22.14.6.Cars going faster than 55, ordered by PetName
22.14.7.Simple linq