Shalini Raj
Shalini Raj

Reputation: 307

Add a list in parent list on basis of parameter

There are 2 classes :

public class RiseData{
public int AccountId{get;set;}
public List<CashData> CashList{get;set;} //and other params
}

public class CashData{
public int AccountId{get;set;}  //and other params
}

I am getting some data in form as below:

List<RiseData> distinctRiseData = riseDataList.FindAll(x => x.RecordType == "P").ToList();
                   
List<Cash> cashListDirty = cashList.FindAll(x => x.RecordType == "C").ToList();

I am trying to fill the cashListDirty to distinctRiseData on the basis of where distinctRiseData.Record.AccountId=cashListDirty.Record.AccountId

Lets say there are 10 records in cashListDirty where 2 records have same AccountId as 1 record in distinctRiseData . I should add those 2 records in distinctRiseData record with the same AccountId. Tried this but doesn't works:

foreach(var i in distinctRiseData )
                    {
                        var data = cashListDirty .FindAll(x => x.AccountId == i.AccountId).ToList();
                        i.CashList.Add(data);
                    }

Thanks.

Upvotes: 1

Views: 72

Answers (1)

user1672994
user1672994

Reputation: 10849

You can use linq Join to get the required result

var cashList = distinctRiseData.Join(cashListDirty, d => d.Record.AccountId, c => c.Record.AccountId, (d, c) => new {d, c}).Select(c => c.c);

Read about linq join at here

Upvotes: 2

Related Questions