-2

I want to merge the fields of songDetails and savedSongs together. How do I do this? I have heard of .Zip() but I am unsure on how to use it.

    [HttpPost]
    public ActionResult Upload(IEnumerable<UploadedSong> songDetails)
    {
        IEnumerable<UploadedSong> savedSongs = (IEnumerable<UploadedSong>)(Session["UserSongs"]);            

        return View();
    }

Edit: songDetails and savedSongs have string fields: Category, AlbumName, BandName, however in songDetails these have data. So I want to merge these non-null data fields with the savedSongs null fields in the same index.

DOOManiac
  • 6,066
  • 8
  • 44
  • 67
sitBy
  • 271
  • 5
  • 19

1 Answers1

1

The way ZIP works is similar to the Aggregate method. To be honest, I don't think that is what you want.

I will show you why with an example how ZIP works. Given the A & B are arrays of integers.

var A = new [] { 1, 3 };
var B = new [] { 2, 4 };

Zipping A & B won't produce:

C = {1, 2, 3 ,4};

ZIP in C# simple returns one value after looking at two values. Here is what you can do with ZIP in .Net 4.0;

Let's say we have the following functions:

Max & Min and use Zip on A & B and I want to apply Max or Min. Here would be the results.

Zip(A,B, Max) = { 2, 4}
Zip(A,B, Min) = { 1, 3}

If I had another function called MultiplyFirstBySecond I would get:

Zip(A,B, MultiplyFirstBySecond ) = {2, 12}

ZIP doesn't actually merge as you can see. It behaves more like Aggregate but across two collections instead of one.

What you want is to merge or copy fields from one object to another in your case from Song Details to Uploaded Song.

I have included a small snippet that might help you. You gave little implementation details. I assume you don't want to divulge too much detials about you project and that is fine :)

Anyhow, here is my attempt to help you.

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{ 
    class UploadedSong { 
        public string Author{get;set;}
        public string Name{get;set;}
        public float Length{get;set;}
        public string Path{get;set;}
        public byte[] SizeInBytes{get;set;}
    }

    public void UpdateSongDetails(IEnumerable<UploadedSong> uploadedSongs, IEnumerable<UploadedSong> songDetails) { 
        // Note that this might blow up if you have duplicate song names
        // You gave very little to work with so my preconditions are weak

        var lookupDetails = songDetails.ToDictionary(song => song.Name, song => song);

        foreach(var uploadedSong in uploadedSongs) {
            var key = uploadedSong.Name;
            if(lookupDetails.ContainsKey(key)) {
                var details = lookupDetails[key];
                uploadedSong.Author = details.Author; 
                // ... more details here
            }
        }
    }

    static void Main()
    {
        // I am assuming that song details is something you populate from some reliable source
        // but for simpilicity I will just declare it.
        var songDetails = new List<UploadedSong>(); 
        var uploadedSongs = (IEnumerable<UploadedSong>)(Session["UserSongs"]);
        UpdateSongDetails(uploadedSongs, songDetails);
    }
}

Cheers and good luck!

Alphanso
  • 61
  • 4