Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

c# - 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.

question from:https://stackoverflow.com/questions/65942930/add-a-list-in-parent-list-on-basis-of-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...