4

I have a class as such:

class Item
{
    public string eventName{ get; set; }
    public string performanceTime { get; set; }
}

and I have two lists of data:

List<string> progName = getProgrammingNames();
List<string> progTimes = getProgrammingTimes()

Inside both string lists are data and I would like to merge them to

List<Item> itemList = new List<Item>();

How can I do it?

0

2 Answers 2

9

Use .Zip to get the items together and then project the Item class:

var result = progName.Zip(progTimes, (name, time) => new Item { 
    eventName = name, 
    performanceTime = time }).ToList();

As Zip only returns items with same index if one collection is bigger than other it will be missing those items. In that case you can use a form of full outer join:

var result = from i in Enumerable.Range(0, Math.Max(progName.Count, progTimes.Count))
             join n in progName.Select((item, index) => new { item, index }) on i equals n.index into names
             from n in names.DefaultIfEmpty()
             join t in progTimes.Select((item, index) => new { item, index }) on i equals t.index into times
             from t in times.DefaultIfEmpty()
             select new Item { eventName = n?.item, performanceTime = t?.item };
Sign up to request clarification or add additional context in comments.

6 Comments

if an event name has many performance times, is there anyway to repeat the same event name for a given time
@shikiko - what do you mean? You have two separate collections. How do you tell which item in the first belongs to one in the second?
like for example if event A has multiple performance times eg. Event | Performance Time Event A | 10:50,11:30 Event B | 9:00, 10:00 how do i add them accordingly
Yes but you have one list containing only the names, and a second containing only time. How do you then tell which name is for which time? what do the getProgrammingNames and getProgrammingTimes do?
@shikiko - That is a very different question and I don't really understand the logic of the merging. I suggest try first and then post another question with the description of the 3 lists, some sample data for each and expected result. Also please post it in a way that it compiles - so it is easier to help. If you wish you can comment me and I will be happy to try and help
|
1

A simple For loop is not enough?

        List<string> progName = getProgrammingNames();
        List<string> progTimes = getProgrammingTimes();

        if (progName.Count != progTimes.Count)
            throw new Exception("2 list counts are different");

        List<Item> itemList = new List<Item>();

        for (var l_i = 0; l_i < progName.Count; l_i++)
        {
            itemList.Add(new Item()
            {
                eventName = progName[l_i],
                performanceTime = progTimes[l_i]
            });
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.