Applying LINQ Queries to Collection Objects in C#

class Bike
{
  public string Name {get; set;}
  public string Color {get; set;}
  public int Speed {get; set;}
  public string Make {get; set;}
}
static void Main(string[] args)
{
  Console.WriteLine("***** LINQ over Generic Collections *****\n");
 
  // Make a List<> of Bike objects.
  List<Bike> myBikes = new List<Bike>() {
    new Bike{ PetName = "bike1", Color = "Silver", Speed = 100, Make = "BMW"},
    new Bike{ PetName = "bike2", Color = "Tan", Speed = 90, Make = "BMW"},
    new Bike{ PetName = "bike3", Color = "Black", Speed = 55, Make = "Yamaha"},
    new Bike{ PetName = "bike4", Color = "Rust", Speed = 5, Make = "Bullet"},
    new Bike{ PetName = "bike5", Color = "White", Speed = 43, Make = "Kawazaki"}
  };
 
// Find all Bike objects in the List<>, where the Speed is
  // greater than 55.
  var fastBikes = from c in myBikes where c.Speed > 55 select c;
 
  foreach (var car in fastBikes)
  {
    Console.WriteLine("{0} is going too fast!", Bike.Name);
  }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.