0

在这种情况下,如果地址 line2 退出,我会尝试更改它。我试图访问 linq 对象并更改它。有没有更好的方法来做下面的代码?

public IEnumerable<AddressViewModel> GetAddress (long sid)
{
    var data = UnitOfWork.DbContext.PAddresses.Where(x => x.PId == sid)
        .Select(x => new AddressViewModel
        {
            PId = sid,
            AddressLine1 = x.Address.AddressLine1,
            AddressLine2 = x.Address.AddressLine2,
            Suburb = x.Address.Suburb.Name,
         });
    for (var i=0; i<=1 ;i++ )
    {
        if (data.ElementAt(i).AddressLine2 != null && data.ElementAt(i).AddressLine2.ToLower() == data.ElementAt(i).Suburb.ToLower())
        {
            data.ElementAt(i).AddressLine2 = null;
        }
        if (data.ElementAt(i).AddressLine3 != null && data.ElementAt(i).AddressLine3.ToLower() == data.ElementAt(i).Suburb.ToLower())
        {
            data.ElementAt(i).AddressLine3 = null;
        }
    }

    return data;
}
4

5 回答 5

0

在您的情况下不需要循环。您将始终循环 1 次。

您可以直接访问该元素:

if (data.ElementAt(i).AddressLine2 != null && data.ElementAt(i).AddressLine2.ToLower() == data.ElementAt(i).Suburb.ToLower())
                {
                    data.ElementAt(i).AddressLine2 = null;
                }

作为旁注,我还会检查它data是否为 null 并且包含元素,否则您可能会遇到ArgumentNullExceptionorArgumentOutOfRangeException异常。

于 2013-06-18T09:43:59.457 回答
0
    public IEnumerable<AddressViewModel> GetAddress (long sid)
            {
                PAddresses data = UnitOfWork.DbContext.PAddresses.Where(x => x.PId == sid)
                                     .Select(x => new AddressViewModel
                                         {
                                             PId = sid,
                                             AddressLine1 = x.Address.AddressLine1,
                                             AddressLine2 = x.Address.AddressLine2,
                                             Suburb = x.Address.Suburb.Name,
                                         }).singleordefault();

if(data != null )
{
 data.ElementAt(i).AddressLine2 = null;  data.ElementAt(i).AddressLine3 = null;

}
                return data;
        }
于 2013-06-18T09:46:35.687 回答
0

获取地址

var address = UnitOfWork.DbContext.PAddresses.Where(x => x.PId == sid)
                        .Select(x => new AddressViewModel {
                            PId = sid,
                            AddressLine1 = x.Address.AddressLine1,
                            AddressLine2 = x.Address.AddressLine2,
                            Suburb = x.Address.Suburb.Name,
                         }).FirstOrDefault();

更改它(如果地址不是null):

if (address.AddressLine2 != null && 
    address.AddressLine2.ToLower() == address.Suburb.ToLower())
    address.AddressLine2 = null;

if (address.AddressLine3 != null &&
    address.AddressLine3.ToLower() == address.Suburb.ToLower())
    address.AddressLine3 = null;

return address;

但更好的方法是遵循告诉不问原则——而不是询问地址属性并根据它们的值进行操作,而是告诉地址进行自我更新。将方法添加到AddressViewModel

public void RemoveDuplicates() // think about better name
{
     if (IsDuplicateOfSuburb(AddressLine2))
         AddressLine2 = null;

     if (IsDuplicateOfSuburb(AddressLine3))
         AddressLine3 = null;
}

private bool IsDuplicateOfSuburb(string addressLine)
{
    if (addressLine == null)
        return false;

    return (addressLine.ToLower() == Suburb.ToLower();
}

并在您的地址对象上调用它:

var address = // get from db context
if (address != null)
    address.RemoveDuplicates();
return address;

干净多了,是吗?

于 2013-06-18T09:48:04.067 回答
0

为什么不在 lambda 内部进行检查?

public IEnumerable<AddressViewModel> GetAddress (long sid)
{
    var data = UnitOfWork.DbContext.PAddresses.Where(x => x.PId == sid)
                         .Select(x => new AddressViewModel
                                      {
                                          PId = sid,
                                          AddressLine1 = x.Address.AddressLine1,
                                          AddressLine2 = x.Address.AddressLine2.ToLower() == x.Address.Suburb.Name.ToLower() ? null : x.Address.AddressLine2,
                                          Suburb = x.Address.Suburb.Name,
                                      });
    return data;
}
于 2013-06-18T09:49:33.500 回答
0

作为投影的一部分,您可以有效地做到这一点。假设您只能有 1 个地址(这就是它的样子),您可以这样做

return UnitOfWork.DbContext.PAddresses.SingleOrDefault(x => x.PId == sid)
       .Select(x => new AddressViewModel
       {
           PId = sid,
           AddressLine1 = x.Address.AddressLine1,
           AddressLine2 = String.Compare(x.Address.AddressLine2, x.Address.Suburb.Name, StringComparison.OrdinalIgnoreCase) == 0 ? null : x.Address.AddressLine2,
           Suburb = x.Address.Suburb.Name,
       });

检查无关紧要,真正的null问题是null如果它与Suburb.Name.

于 2013-06-18T09:51:50.447 回答