Freigeben über


Refreshing my Linq skills

... and just thought I could share my experience with you.

My training in the US is over and I’ve been back in Sweden for two weeks. It’s been two busy weeks with several customer meetings and an ALM Workshop… and ALM is Application Lifecycle Management. My fingers are itching to write some code, so here we go…

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Linq;
    4:  
    5: namespace AI.Linq01
    6: {
    7:     class Contact
    8:     {
    9:         // New Feature: Auto-Implemented Properties
   10:         public int ID { get; set; }
   11:         public string Name { get; set; }
   12:     }
   13:  
   14:     class Program
   15:     {
   16:         static void Main(string[] args)
   17:         {
   18:             // New Feature: Implicity Typed Local Variable
   19:             var contacts = new List<Contact>
   20:             {
   21:                 // New Feature: Object Initializer
   22:                 new Contact(){ID = 1, Name = "Kristofer"},
   23:                 new Contact(){ID = 2, Name = "Qiang"},
   24:                 new Contact(){ID = 3, Name = "Scott"}
   25:             };
   26:  
   27:             // New Feature: Linq Query
   28:             // Retriev every contact with ID less than 2
   29:             var result1 = from contact in contacts
   30:                            where contact.ID < 2
   31:                            select contact;
   32:  
   33:             // Write the result to the Console
   34:             foreach (var c in result1) 
   35:                 Console.WriteLine(c.Name);
   36:  
   37:             // Wait for a key to be pressed
   38:             Console.ReadKey();
   39:         }
   40:     }
   41: }

Output of this program is:

Kristofer

Doesn’t seem so much, but I’ll dig deeper and deeper into Linq in the upcoming blog posts.

blogStream.Close();