user3228992
user3228992

Reputation: 1373

Merge 2 separate lists of different types into 1 new list of another type

I'm getting json-data from two separate API:s and then putting them into two lists of separate types. So far all is good, like below:

// Instagram
var instaService = new InstagramService();
var instaObj = await instaService.GetInstagramObject("my company", searchOpt.MinMaxId);

// Flickr
var options = new PhotoSearchOptions { PerPage = 20, Page = 1, UserId = "*************" };
PhotoCollection photos = FlickrManager.GetInstance().PhotosSearch(options);

But then I need to put them together into another list (of another type), to be showed in GUI ordered by date.

Below is what I've achieved so far. But when 'AddRange' 'var media' is empty. What's wrong here. I want 'var media' to hold values, now they ar null.

foreach (var item in instaObj.items)
{
    var media = model.ImportedMedia.Select(o => new MediaImport
    {
        Id = item.id,
        MediaUrl = item.images.standard_resolution.url,
        CreatedTime = item.created_time
    }).ToList();

    model.ImportedMedia.AddRange(media);

}

foreach (var item in photos)
{
    var media = model.ImportedMedia.Select(o => new MediaImport
    {
        Id = item.PhotoId,
        MediaUrl = item.Medium640Url,
        CreatedTime = item.DateUploaded.ToString(CultureInfo.InvariantCulture)
    }).ToList();

    model.ImportedMedia.AddRange(media);
}

Upvotes: 1

Views: 69

Answers (3)

Simon Karlsson
Simon Karlsson

Reputation: 4129

You probably want to operate on your collections that you are iterating instead like this

var media1 = instaObj.items.Select(o => new MediaImport
{
    Id = o.id,
    MediaUrl = o.images.standard_resolution.url,
    CreatedTime = o.created_time
});

var media2 = photos.Select(o => new MediaImport
{
    Id = o.PhotoId,
    MediaUrl = o.Medium640Url,
    CreatedTime = o.DateUploaded.ToString(CultureInfo.InvariantCulture)
});

var mergedList = media1.Union(media2).ToList();

Currently it feels a bit like you are mixing apples and pears.

Upvotes: 3

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

you are creating the media variables from model.ImportedMedia itself, then adding it. If model.ImportedMedia is initially empty, then no elements will be added.

I think what you want is:

var media1 = instaObj.items.Select(item => new MediaImport
{
    Id = item.id,
    MediaUrl = item.images.standard_resolution.url,
    CreatedTime = item.created_time
});

var media2 = photos.Select(item => new MediaImport
{
    Id = item.PhotoId,
    MediaUrl = item.Medium640Url,
    CreatedTime = item.DateUploaded.ToString(CultureInfo.InvariantCulture)
});

model.ImportedMedia.AddRange(media1);
model.ImportedMedia.AddRange(media2);

Upvotes: 3

René Vogt
René Vogt

Reputation: 43876

Why are you calling Select on model.ImportedMedia? I think what you want to do is this:

model.ImportedMedia.AddRange(instaObj.items.Select(item =>
    new MediaImport {
        Id = item.id,
        MediaUrl = item.images.standard_resolution.url,
        CreatedTime = item.created_time
    });

followed by

model.ImportedMedia.AddRange(photos.Select(photo => new MediaImport
    {    
        Id = photo.PhotoId,
        MediaUrl = photo.Medium640Url,
        CreatedTime = photo.DateUploaded.ToString(CultureInfo.InvariantCulture)
});

Upvotes: 4

Related Questions