2016-10-22 6 views
0

私はリストにオブジェクトを挿入する方法を見つけるのに時間を費やしてきました。 これは私がやったことです。あなたは私のを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は

おかげ

+1

オブジェクトを塗りつぶすのに必要なリストdata2を反復処理していますか?リストdata2からレコードを見つけ出すことはできず、オブジェクトを反復せずに値を設定することはできますか? – gkb

+0

ええと、foreachなしでの意味ですか? – Webster

+0

Yess ...ちょうど行をつかんでオブジェクトを挿入する... – gkb

答えて

0

は、ArrayListにあなたのリストを変換し、指定されたインデックスに挿入する方法で構築された使用することができ、みんなが理解されるであろう。

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

残念です、その仲間のようなものではありません – Webster

+0

あなたは値を挿入するときあなたのリストでどのような条件をお探しですか? –

0

Skip()とTake()の使用はどうですか?

省略したい行の数をスキップしてから、1つの要素を取ります。 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()拡張は必要ないかもしれません。

+0

行は、 - d.paymentdistributionid == i.paymentdistributionidのような条件に基づいて取得する必要があります。いくつかのオフセット値ではなく...正確な行をフェッチするためにyourRowIndexを動的に変更する方法はありますか? – gkb

関連する問題