如何使用linq查询对象的子对象

本文关键字:对象 查询 何使用 linq | 更新日期: 2023-09-27 18:02:47

我有一个Customer对象。Customer对象可能包含多个Address对象。

例如,我想将Customer对象上的方法设置为GetCity

现在,每个Address对象都有一个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
}

当我在Customer对象上运行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;
    }

我知道Customer对象中确实包含Address对象,但我不确定如何使GetCity方法发挥作用。

如有任何帮助,我们将不胜感激。如果我遗漏了任何重要信息,请告诉我。

如何使用linq查询对象的子对象

这里的问题是_addresss集合根本不包含任何元素,或者_addresses集合不包含AddressID等于id的任何元素(可能更可能(。

Single()的工作原理是,如果符合给定条件的项目少于1(0(或多于一个,则它会抛出异常。如果您只是想在没有匹配项时返回null,请使用SingleOrDefault()

where a.AddressID == id

正在过滤掉您的所有地址,因此当您在空枚举上调用Single时会收到错误消息。

您可以将Single切换到SingleOrDefault,但我认为主要问题是地址比较

linq查询似乎没有返回任何填充List的项?你确定请求的id存在吗?

Single需要一个只有1个项目的IEnumerable