c# - 将对象添加到列表上的特定索引

时间:2016-10-22 04:18:12

标签: c# linq list

我花了好几个小时找到一种方法将项目插入我的列表中的对象。 这就是我所做的。

data = await (from iw in _context.InvestorWallets
                              join m in memberdata on iw.investorid equals m.id
                              where _context.InvestorDeposit.Any(item => item.investordepositid == iw.transactionid) ||
                             _context.InvestorWithdrawal.Any(item => item.withdrawalid == iw.transactionid) ||
                             _context.InstallmentPaymentDistribution.Any(item => item.paymentdistributionid == iw.transactionid)
                              select new InvestorWalletsViewModel()
                              {
                                  username = m.userName,
                                  fullname = m.fullName,
                                  isActive = m.isActive,
                                  memberType = m.memberType,
                                  memberTypeName = m.memberTypeName,
                                  emailAddress = m.emailAddress,
                                  trxdate = iw.trxdate,
                                  trxdesc = iw.trxdesc,
                                  debit = iw.acounttype.ToLower() == "db" ? iw.amount : 0,
                                  credit = iw.acounttype.ToLower() == "cr" ? iw.amount : 0,
                                  investorid = iw.investorid,
                                  investorwalletid = iw.investorwalletid,
                                  transactiontype =
                                    (
                                        iw.transactiontype.ToLower() == "dep" ? "Deposit" :
                                        iw.transactiontype.ToLower() == "wit" ? "Withdrawal" :
                                        iw.transactiontype.ToLower() == "rep" ? "IB-1610043 - Repayment" : "REGULER"
                                    ),
                                  paymentdistributionid =
                                    (
                                        iw.transactiontype.ToLower() == "rep" ? iw.transactionid??0 : 0
                                    ),
                                  details = new List<InvestorWalletsViewModelDetail>(), // <-- I want to insert some object to this list
                                  previousbalance = iw.previousbalance
                              }).Distinct().ToListAsync();

//This is to retrieve data from another table, based on data
var data2 = (from i in _context.InstallmentPaymentDistributionDetails.Where(p => p.amount > 0)
                             where data.Any(d => d.paymentdistributionid == i.paymentdistributionid)
                             select i).Distinct().ToList();

//I used ForEach to itterate through the List, and fill the object
data2.ForEach(d =>
{
    var x = data.Find(z => z.paymentdistributionid == d.paymentdistributionid);
    if (x == null) return;

//This is where I insert the value to the List
    x.details.Add(new InvestorWalletsViewModelDetail //This code right here, it insert the value to all rows in the List, i just want to add the value to the specific row in the List
    {
        actualpayment = d.amount,
        installmentfeetype_name = d.installmentfeetype_name
    });
});

}

正如您在 ForEach 中看到的那样,我将值插入到详细信息中(列表中的列表)。但是,它确实是,它确实将值插入List,但它将值插入List中的 ALL 行,而它只应将值插入特定的行/索引。我只想将值插入List中的特定行

Anyhelp将受到赞赏,

由于

2 个答案:

答案 0 :(得分:0)

可以将列表转换为arraylist,然后使用内置方法插入给定索引。

ArrayList arrayList = new ArrayList(list);
arrayList.insert(index, val);

答案 1 :(得分:0)

如何使用Skip()和Take()?

您跳过了要省略的行数,然后取1个元素。这将返回一个包含一个元素的对象列表。

int yourRowIndex = 47; // just an example
x.details.Skip(yourRowIndex).Take(1).ToList().Add(new InvestorWalletsViewModelDetail
{
    actualpayment = d.amount,
    installmentfeetype_name = d.installmentfeetype_name
});

可能不需要ToList()扩展名。