Zip Lists Resulting in Instantiation of Object and Adding to Collection

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Zip Lists Resulting in Instantiation of Object and Adding to Collection



I have a class Song with properties Title and URL.


Song


Title


URL



I also have two List<string> collections, titles and URLs, and one List<Song> collection, songs.


List<string>


titles


URLs


List<Song>


songs



I want to zip titles and URLs using Linq Zip() and for each iteration instantiating a new song object with the iterated title and URL as its corresponding properties and add the result to songs. As such:


titles


URLs


songs



List<Song> songs = titles.Zip(URLs, (currentTitle, currentURL) => songs.Add(new Song Title = currentTitle, URL = currentURL);


List<Song> songs = titles.Zip(URLs, (currentTitle, currentURL) => songs.Add(new Song Title = currentTitle, URL = currentURL);



I get the following error:



Error CS0411 The type arguments for method 'Enumerable.Zip(IEnumerable, IEnumerable, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. OldRedditMusicScraper



Is it possible to do what I'm trying to do with a fix or workaround?



Thanks in advance.



Regards eheu




1 Answer
1



You can make a List like so:



var songs = titles.Zip<string,string,Song>(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL );


var songs = titles.Zip<string,string,Song>(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL );



you could also omit the type information so:



var songs = titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL );


var songs = titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL );



If songs was an existing list you could make it work like so:
songs.AddRange( titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL ) );


songs.AddRange( titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL ) );



After looking at your edit, looks like you may want to do something like this perhaps:
List<Song> songs = titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL ).ToList();


List<Song> songs = titles.Zip(URLs, (currentTitle, currentURL) => new Song Title = currentTitle, URL = currentURL ).ToList();





Good answer, I understand the method a lot better now. Thanks so much!
– eheu
Aug 10 at 16:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard