私はリストにオブジェクトを挿入する方法を見つけるのに時間を費やしてきました。 これは私がやったことです。あなたは私のをForEach上で見ることができるようにc# - リスト上の特定のインデックスにオブジェクトを追加する
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
});
});
}
は、私は(リスト内のリスト)詳細に値を挿入します。しかし、何が起こっているかは、Listに値を挿入しましたが、特定の行/インデックスにのみ値を挿入する必要がある場合は、ALLの値をListに挿入しました。私はちょうどその値を特定の行にリストに挿入したいと思います。
Anyhelpは
おかげ
オブジェクトを塗りつぶすのに必要なリストdata2を反復処理していますか?リストdata2からレコードを見つけ出すことはできず、オブジェクトを反復せずに値を設定することはできますか? – gkb
ええと、foreachなしでの意味ですか? – Webster
Yess ...ちょうど行をつかんでオブジェクトを挿入する... – gkb