简体   繁体   中英

Error Index was out of range. Must be non-negative and less than the size of the collection

I am trying to fill my DTO objects with for, but I got this error:

Index was out of range. Must be non-negative and less than the size of the collection

Here is my code:

 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;

        }

I got this message at here

bdto[i].AccountId = transactionlist[i].AccountId;

You've created an empty list, and aren't adding elements to it. You must first add an element, and then update its properties:

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);
}

Well obvously, bdto length is less than transactionlist length.

before your for loop you can resize bdto to match transactionlist

I totally agree with @Ashkan and @Mureinik's answer, but just to improve code you can use AutoMapper . It will reduce loop and iteration for every element. It seems that all the properties between these two objects is the same.

Mapper.CreateMap<Transaction,BankDepositHistoryDTO>(); 
BankDepositHistoryDTO obj = Mapper.Map<Transaction,BankDepositHistoryDTO>(TransactionObj);

Also if GetListoftransactions.GetAccountingListoftransactions(); returns List<BankDepositHistoryDTO> , you can directly return that object in method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM