Reputation: 1574
I have a Customer object. The Customer object may contain multiple Address objects.
I would like to set up a method on the Customer object to GetCity
for example.
Now, each Address object has an addressId so I'm trying to query the property via linq.
Below is my code (i've cut it down for this issue)
the Customer object...
public class Customer
{
private string _customerNo;
private List<CustomerAddress> _addresses = new List<CustomerAddress>();
//other members immitted from this sample
//constructor code, immitted from this sample
public List<CustomerAddress> 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
}
When I run the GetCity(id)
method on the Customer object I get the error.
System.InvalidOperationException: Sequence contains no elements
I also tried the following code in the linq query but got the same results.
public string GetCity(string id)
{
var city = (from a in this.Addresses
where a.AddressID == id
select a.City).Single();
return city;
}
and...
public string GetCity(string id)
{
var city = (from a in this._addresses
where a.AddressID == id
select a.City).Single();
return city;
}
I know that the Customer object does contain Address objects within it, but i'm not sure how to get the GetCity
Method to work.
Any help would be appreciated. If i've missed any important info please let me know.
Upvotes: 2
Views: 1593
Reputation: 26694
where a.AddressID == id
Is filtering out all your addresses, so you get that error message when you call Single
on an empty enumeration.
You can switch Single
to SingleOrDefault
, but I think the main issue is with the address comparison
Upvotes: 3
Reputation: 12431
The problem here is either that the _addresses collection does not contain any elements at all, or that the _addresses collection does not contain any elements with the AddressID
equal to id (probably more likely).
How Single()
work is that it throws an exception if there is either fewer than 1 (0) or more than one item that match your given criteria. If you simply want to return null
when there are no matches use SingleOrDefault()
.
Upvotes: 4
Reputation: 709
It seems that the linq query is not returning any items where do you populate the List? are you sure that the requested id exists?
Single needs a IEnumerable with exactly 1 item in it
Upvotes: 1