将对象添加到列表上的特定索引
本文关键字:索引 列表 对象 添加 | 更新日期: 2023-09-27 18:14:33
我已经花了几个小时寻找一种方法来插入项目到我的列表中的对象。这就是我所做的。
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行,而它应该只将值插入到特定的行/索引中。我只是想将值插入到特定行中的列表。任何帮助都将是感激的家伙,
谢谢
可以将列表转换为数组列表,然后使用内置的方法在给定的索引处插入。
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()扩展