使用来自相关实体的值填充POCO对象,该值可能为null以填充字符串

时间:2014-02-27 23:45:05

标签: entity-framework

我填充POCO,我用它来返回调用者以使用绑定到控件,在Web表单上显示等。如果其中一个实体为null,那么尝试读取其相关实体值之一将导致例外。

它的字段如“DestinationState”,在DB中可以为空,但我的POCO类将其定义为字符串,您无法将其定义为可为空。所以在我的情况下,我对每个潜在的问题都进行了一些测试,并将其设置为空字符串。

以下是如何处理它。这有效,但似乎应该有更好的方法?

        OrderInfo retval = new OrderInfo();

        //find order
        Order o = ctx.Orders.Where(a => a.OrderId == oid).First();
        if (o != null)
        {
            //now find the locations
            Location oloc = o.Locations.Where(b => b.OrderKey == o.OrderId).Where(c => c.LocationType.TypeName == "Origin").FirstOrDefault();
            Location dloc = o.Locations.Where(b => b.OrderKey == o.OrderId).Where(c => c.LocationType.TypeName == "Destination").FirstOrDefault();


            retval.CreatedBy = o.User.UserName;
            retval.AccountName = o.Account.AccountName;
            retval.CustomerName = o.CustomerName;
            retval.OfficeName = o.Office.OfficeName;
            retval.DateCreated = o.DateCreated;
            retval.OriginCity = oloc.City == null ? "" : oloc.City;
            retval.OriginState = oloc.State == null ? "" : oloc.State.StateName;
            retval.OriginCountry = oloc.Country == null ? "" : oloc.Country.CountryName;
            retval.AccountRef = o.AccountRefNumber;
            retval.DestinationCity = dloc.City == null ? "" : dloc.City;
            retval.DestinationState = dloc.State == null ? "" : dloc.State.StateName;
            retval.DestinationCountry = dloc.Country == null ? "" : dloc.Country.CountryName;

        }

        return retval;

1 个答案:

答案 0 :(得分:0)

您可以使用这样的速记(只要您不访问导航属性):

retval.OriginCity = oloc.City ?? string.Empty;

您还可以通过将重复访问的值存储在变量中并使用对象初始值设定项来使其更清晰:

    //find order
    Order o = ctx.Orders.Where(a => a.OrderId == oid).First();
    if (o != null)
    {
        //now find the locations
        Location oloc = o.Locations.Where(b => b.OrderKey == o.OrderId).Where(c => c.LocationType.TypeName == "Origin").FirstOrDefault();
        Location dloc = o.Locations.Where(b => b.OrderKey == o.OrderId).Where(c => c.LocationType.TypeName == "Destination").FirstOrDefault();

        // Store variables that will be used repeatedly
        string cityName = oloc.City ?? string.Empty;
        string stateName = dloc.State == null ? string.Empty : dloc.State.StateName;
        string countryName = dloc.Country == null ? string.Empty : dloc.Country.CountryName;

        // Create object using object initializer and return
        return new OrderInfo {
            CreatedBy = o.User.UserName;
            AccountName = o.Account.AccountName;
            CustomerName = o.CustomerName;
            OfficeName = o.Office.OfficeName;
            DateCreated = o.DateCreated;
            OriginCity = cityName;
            OriginState = stateName;
            OriginCountry = countryName;
            AccountRef = o.AccountRefNumber;
            DestinationCity = cityName;
            DestinationState = stateName;
            DestinationCountry = countryName;
        };
    }
    else 
    {
        return new OrderInfo();
    }