title: how to query a sub object of an object with linq
body: <p>I have a Customer object. The Customer object may contain multiple Address objects.</p>
  
  <p>I would like to set up a method on the Customer object to <code>GetCity</code> for example.</p>
  
  <p>Now, each Address object has an addressId so I'm trying to query the property via linq.</p>
  
  <p>Below is my code (i've cut it down for this issue)</p>
  
  <p>the Customer object...</p>
  
  <pre><code>public class Customer
  {
      private string _customerNo;
      private List&lt;CustomerAddress&gt; _addresses = new List&lt;CustomerAddress&gt;();
      //other members immitted from this sample
  
  
      //constructor code, immitted from this sample
  
      public List&lt;CustomerAddress&gt; Addresses
      {
          get { return _addresses; }
      }
      //other properties immitted from this sample    
  
      public string GetCity(string id)
      {
          var city = (from a in _addresses
                          where a.AddressID == id
                          select a.City).Single();
  
          return city;
      }   
  }
  
  the Address Object...
  public class CustomerAddress
  {
      private string _addressid;
      private string _city;
      //other members immitted from this sample
  
      public CustomerAddress(string strAddressID, string strAddressLister, string strAddress1, string strAddress2, string strAddress3, string strCity, string strState, string strPostCode, string strCountry,
          bool booDefaultAddress, string strDeliveryAddress, string strInvoiceAddress, string strPayAddress, string strVisitAddress, string strDeliveryTerms, string strShipVia, string strRouteId)
      {
          _addressid = strAddressID;
          _addresslister = strAddressLister;
          _address1 = strAddress1;
          _address2 = strAddress2;
          _address3 = strAddress3;
          _city = strCity;
          _state = strState;
          _postcode = strPostCode;
          _country = strCountry;
          _defaultaddress = booDefaultAddress;
          _deliveryaddress = strDeliveryAddress;
          _invoiceaddress = strInvoiceAddress;
          _payaddress = strPayAddress;
          _visitaddress = strVisitAddress;
          _deliveryterms = strDeliveryTerms;
          _shipvia = strShipVia;
          _routeid = strRouteId;
      }
  
      ////////////////////////////
      //CustomerAddress Properties
      ////////////////////////////
      public string AddressID
      {
          get { return _addressid; }
      }    
      public string City
      {
          get { return _city; }
      }
      //other properties immitted from this sample
  }
  </code></pre>
  
  <p>When I run the <code>GetCity(id)</code> method on the Customer object I get the error.</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Sequence contains no elements</p>
  </blockquote>
  
  <p>I also tried the following code in the linq query but got the same results.</p>
  
  <pre><code>public string GetCity(string id)
  {
      var city = (from a in this.Addresses
                      where a.AddressID == id
                      select a.City).Single();
  
      return city;
  }
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>public string GetCity(string id)
      {
          var city = (from a in this._addresses
                          where a.AddressID == id
                          select a.City).Single();
  
          return city;
      }
  </code></pre>
  
  <p>I know that the Customer object does contain Address objects within it, but i'm not sure how to get the <code>GetCity</code> Method to work.</p>
  
  <p>Any help would be appreciated. If i've missed any important info please let me know.</p>