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.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was 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 Microsoft MVP.
This entry was posted in Linq, Mini-Tutorial and tagged . Bookmark the permalink.

2 Responses to Linq with Objects

Comments are closed.