错误索引超出范围。必须是非负数且小于集合的大小
Error Index was out of range. Must be non-negative and less than the size of the collection
我正在尝试用 for 填充我的 DTO 对象,但出现此错误:
Index was out of range. Must be non-negative and less than the size of the collection
这是我的代码:
public static List <BankDepositHistoryDTO> DtoTODomain()
{
MyketAdsEntities context = new MyketAdsEntities();
List<BankDepositHistoryDTO> bdto = new List<BankDepositHistoryDTO>();
//var transactionlist
var transactionlist = GetListoftransactions.GetAccountingListoftransactions();
for (int i = 0; i < transactionlist.Count; i++)
{
bdto[i].AccountId = transactionlist[i].AccountId;
bdto[i].Id = transactionlist[i].Id;
bdto[i].Amount = transactionlist[i].Amount;
bdto[i].AdditionalData = transactionlist[i].AdditionalData;
bdto[i].ClientIp = transactionlist[i].ClientIp;
bdto[i].Gateway = transactionlist[i].Gateway;
bdto[i].PaymentRefNumber = transactionlist[i].PaymentRefNumber;
bdto[i].ReturnUrl = transactionlist[i].ReturnUrl;
bdto[i].State = transactionlist[i].State;
bdto[i].Uuid = transactionlist[i].Uuid;
}
return bdto;
}
我在这里收到这条消息
bdto[i].AccountId = transactionlist[i].AccountId;
显然,bdto
长度小于 transactionlist
长度。
在 for 循环之前,您可以调整 bdto
的大小以匹配 transactionlist
您创建了一个空列表,并且没有向其中添加元素。您必须先添加一个元素,然后更新其属性:
for (int i = 0; i < transactionlist.Count; i++)
{
BankDepositHistoryDTO b = new BankDepositHistoryDTO();
b.AccountId = transactionlist[i].AccountId;
b.Id = transactionlist[i].Id;b
b.Amount = transactionlist[i].Amount;
b.AdditionalData = transactionlist[i].AdditionalData;
b.ClientIp = transactionlist[i].ClientIp;
b.Gateway = transactionlist[i].Gateway;
b.PaymentRefNumber = transactionlist[i].PaymentRefNumber;
b.ReturnUrl = transactionlist[i].ReturnUrl;
b.State = transactionlist[i].State;
b.Uuid = transactionlist[i].Uuid;
bdto.Add(b);
}
我完全同意@Ashkan 和@Mureinik 的回答,但只是为了改进代码,您可以使用 AutoMapper。它将减少每个元素的循环和迭代。似乎这两个对象之间的所有属性都是相同的。
Mapper.CreateMap<Transaction,BankDepositHistoryDTO>();
BankDepositHistoryDTO obj = Mapper.Map<Transaction,BankDepositHistoryDTO>(TransactionObj);
另外,如果GetListoftransactions.GetAccountingListoftransactions();
returns List<BankDepositHistoryDTO>
,你可以直接return方法中的那个对象。
我正在尝试用 for 填充我的 DTO 对象,但出现此错误:
Index was out of range. Must be non-negative and less than the size of the collection
这是我的代码:
public static List <BankDepositHistoryDTO> DtoTODomain()
{
MyketAdsEntities context = new MyketAdsEntities();
List<BankDepositHistoryDTO> bdto = new List<BankDepositHistoryDTO>();
//var transactionlist
var transactionlist = GetListoftransactions.GetAccountingListoftransactions();
for (int i = 0; i < transactionlist.Count; i++)
{
bdto[i].AccountId = transactionlist[i].AccountId;
bdto[i].Id = transactionlist[i].Id;
bdto[i].Amount = transactionlist[i].Amount;
bdto[i].AdditionalData = transactionlist[i].AdditionalData;
bdto[i].ClientIp = transactionlist[i].ClientIp;
bdto[i].Gateway = transactionlist[i].Gateway;
bdto[i].PaymentRefNumber = transactionlist[i].PaymentRefNumber;
bdto[i].ReturnUrl = transactionlist[i].ReturnUrl;
bdto[i].State = transactionlist[i].State;
bdto[i].Uuid = transactionlist[i].Uuid;
}
return bdto;
}
我在这里收到这条消息
bdto[i].AccountId = transactionlist[i].AccountId;
显然,bdto
长度小于 transactionlist
长度。
在 for 循环之前,您可以调整 bdto
的大小以匹配 transactionlist
您创建了一个空列表,并且没有向其中添加元素。您必须先添加一个元素,然后更新其属性:
for (int i = 0; i < transactionlist.Count; i++)
{
BankDepositHistoryDTO b = new BankDepositHistoryDTO();
b.AccountId = transactionlist[i].AccountId;
b.Id = transactionlist[i].Id;b
b.Amount = transactionlist[i].Amount;
b.AdditionalData = transactionlist[i].AdditionalData;
b.ClientIp = transactionlist[i].ClientIp;
b.Gateway = transactionlist[i].Gateway;
b.PaymentRefNumber = transactionlist[i].PaymentRefNumber;
b.ReturnUrl = transactionlist[i].ReturnUrl;
b.State = transactionlist[i].State;
b.Uuid = transactionlist[i].Uuid;
bdto.Add(b);
}
我完全同意@Ashkan 和@Mureinik 的回答,但只是为了改进代码,您可以使用 AutoMapper。它将减少每个元素的循环和迭代。似乎这两个对象之间的所有属性都是相同的。
Mapper.CreateMap<Transaction,BankDepositHistoryDTO>();
BankDepositHistoryDTO obj = Mapper.Map<Transaction,BankDepositHistoryDTO>(TransactionObj);
另外,如果GetListoftransactions.GetAccountingListoftransactions();
returns List<BankDepositHistoryDTO>
,你可以直接return方法中的那个对象。