2013-03-06 47 views
2

我有一個Customer對象。 Customer對象可能包含多個Address對象。如何用linq查詢對象的子對象

我想客戶對象上的方法設置爲GetCity例如。

現在,每個地址對象具有addressId所以我想通過LINQ查詢性能。

下面是我的代碼(我已經砍了這個問題)

Customer對象...

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 
} 

當我在客戶對象上運行GetCity(id)方法我得到的錯誤。

System.InvalidOperationException:序列不包含任何元素

我也試過在LINQ查詢下面的代碼,但得到了同樣的結果。

public string GetCity(string id) 
{ 
    var city = (from a in this.Addresses 
        where a.AddressID == id 
        select a.City).Single(); 

    return city; 
} 

和...

public string GetCity(string id) 
    { 
     var city = (from a in this._addresses 
         where a.AddressID == id 
         select a.City).Single(); 

     return city; 
    } 

我知道,客戶對象確實包含在它的地址對象,但我不知道如何讓GetCity法工作。

任何幫助,將不勝感激。如果我錯過了任何重要信息,請讓我知道。

+0

您可以發佈一個Customer對象,以不會忽略? – tschmit007 2013-03-06 15:07:21

回答

4

這裏的問題是,要麼在_addresses集合不可言,包含任何元素的_addresses集合不包含與AddressID的任何元素等於id(可能更可能)。

Single()工作如何爲它拋出一個異常,如果有任何少於1(0)或符合您給定的標準不止一個項目。如果您只是想在沒有匹配的情況下使用SingleOrDefault()返回null

+0

我認爲每個人都有一個觀點......但這個答案引發了一個想法......我認爲我傳入方法後的id後面有空格,我在字符串上使用了Trim()方法,現在我正在獲得價值。 每個客戶對象至少有一個地址對象,所以地址永遠不會是空的,這是讓我困惑的地方......該ID是源於具有特定字段長度的SQL源,所以這需要修剪:) 感謝您的迴應:) – Stuart 2013-03-06 16:26:50

1

看來,LINQ查詢沒有返回你在哪裏填充列表的任何項目?你確定所請求的ID存在嗎?

單需要一個IEnumerable的使用正好1個項目在它

3
where a.AddressID == id 

是過濾掉所有你的地址,因此,當你在一個空枚舉調用Single得到這個錯誤消息。

您可以切換到SingleSingleOrDefault,但我認爲主要的問題是與地址比較