0
\$\begingroup\$

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();

    }
}
\$\endgroup\$
1
\$\begingroup\$

It would be better to create a Dictionary<int, Job> of jobs and use it in the foreach loop. Otherwise, you have to parse dataRowView["ID"] field as many times as jobs are available (at worst). If number of jobs is less than 10, it is more performant to iterate the list of jobs instead of using a Dictionary<int, Job>:

//Step 3: Find match, and set the value from dataView to jobs when matched.
    Dictionary<int, Job> jobDict = jobs.ToDictionary(j => j.Id);

    foreach (DataRowView dataRowView in dataView)
    {
        var id = int.Parse(dataRowView["ID"].ToString());
        Job job;
        if (jobDict.TryGet(id, out job)
            //&& other logic here
         )
        {
            //once matched, it sets the value. This is what I need
            job.DiscountDate = (DateTime) dataRowView["DiscountDate"];
            break;
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.