Linq with Objects

A LINQ Tutorial

 

In my earlier post, we took a first look at a Linq query against a collection of integers. In this posting, we’ll look at creating a Linq query against a collection of objects.

We first need an object; we’ll create a new class: Customer.  (To do this, right click on the project and select Add->Class.  Name the class customer.cs).

 public class Customer
 {
    public string FullName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Phone { get; set; }

In addition to the five automatic Properties, we’ll want a property that returns a small collection of Customer objects.  We’ll make this last property static so that we don’t have to have an instance of Customer to obtain it,

public static List<Customer> Customers
{
   get
   {
      var _customers = new List<Customer>( );
      var c = new Customer( )
      {
         FullName = "George Washington",
         Address = "The President's House",
         City = "New York",
         State = "NY",
         Phone = "212-555-5555"
      };
      _customers.Add( c );

      c = new Customer( )
      {
         FullName = "John Adams",
         Address = "The White House",
         City = "Washington",
         State = "DC",
         Phone = "202-555-5555"
      };
      _customers.Add( c );

      c = new Customer( )
      {
         FullName = "Thomas Jefferson",
         Address = "The White House",
         City = "Washington",
         State = "DC",
         Phone = "202-555-5555"
      };
      _customers.Add( c );

      return _customers;
   }
}

You can see that this property returns a collection of Customers, initialized within the property itself to three Customer objects; two of whom reside in Washington, D.C.

We can now return to MainPage.cs and write the code to obtain this collection and then to query against it,

 

 public MainPage( )
 {
    InitializeComponent( );
    List<Customer> customers = Customer.Customers;
    var query = from c in customers
                where c.City == "Washington"
                select new { FullName = c.FullName };

    foreach ( var pres in query )
    {
       TheListBox.Items.Add( pres.FullName );
    }
 }

The logic of the query is almost identical to the logic used in the previous example. It can be read as “from the customers collection find every customer whose city is Washington and select the FullName of that object into a new (unnamed) object.

The foreach loop then takes the results and iterates through each entry, extracting the FullName property that was created in the unnamed object and displaying it in the list box.

Note: These examples are being kept very short to encourage you to type them in and then to play with the results; this is the best way to get a feel for the invariants in Linq.

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in Linq, Mini-Tutorial and tagged . Bookmark the permalink.

3 Responses to Linq with Objects

  1. match.com says:

    I think that everything posted made a great deal of sense.
    But, what about this? suppose you added a little content?
    I mean, I don’t want to tell you how to run your website, however suppose you added a
    title that makes people want more? I mean Linq with Objects | Jesse Liberty is kinda plain.
    You ought to glance at Yahoo’s home page and see how they create post titles to get viewers to click.
    You might try adding a video or a pic or two to get people interested about what you’ve got to say.

    In my opinion, it would make your website a little bit
    more interesting.

  2. test website says:

    Nice post. I was checking continuously this weblog and I am inspired! Very useful information specially the ultimate phase I care for such info much. I was looking for this certain information for a very long time. Thank you and good luck.

  3. Pingback: Tweets that mention Linq with Objects: Windows Phone From Scratch #34

Leave a Reply

Your email address will not be published.