簡體   English   中英

c#-將對象添加到列表中的特定索引

[英]c# - Add Object to specific Index on 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內)。 但是,發生的事情是,它確實將值插入到列表中,但是當它只應將值插入到特定的行/索引時,才將值插入到列表中的所有行。 我只想將值插入列表中的特定行

任何幫助將不勝感激的家伙,

謝謝

可以將您的列表轉換為arraylist,然后使用內置方法在給定索引處插入。

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

如何使用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()擴展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM