在这种情况下,如果地址 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;
}