I want to find common items between two collection, and set values from one collection to another collection. I am aware of similar posts on the web, but they are different from this post. I want to know if there is a better algorithm or method than my current code below
class Job { public Job(int id) { this.Id = id; } public int Id { get; private set; } public DateTime DiscountDate; //more properties } class JobManager { public List<Job> GetJobs() { //Step 1: initialize the first collection List<Job> jobs = new List<Job>() { new Job(1), //more other properties initialization new Job(2), new Job(3), new Job(4) }; //Step 2: get second collection (id, and DiscountDate) by ids. The ids might be part or whole of collection in step 1 List<int> idsForDiscountDate = new List<int> {2, 4}; //all ids must be part or whole of jobs list above, ie 1-4 DataView dataView = GetDisccountDatesFromdatabaseBy(idsForDiscountDate); //all rows must be part or whole of jobs list above, ie 1-4 //Step 3: Find match, and set the value from dataView to jobs when matched. foreach (var job in jobs) { foreach (DataRowView dataRowView in dataView) { if (int.Parse(dataRowView["ID"].ToString()) == job.Id //&& other logic here ) { //once matched, it sets the value. This is what I need job.DiscountDate = (DateTime) dataRowView["DiscountDate"]; break; } } } return jobs; } //get discount dates from database private DataView GetDisccountDatesFromdatabaseBy(List<int> idsForDisccountDate) { throw new NotImplementedException(); } }