I am using an external library thats returns a IEnumerable. After I have recieved them I would like to add some models to the end. That only seems possible when using an IList or some other collection. So when i’m trying to convert the IEnumerable to a list using the .ToList() method it returns an IEnumerable. That’s not the what I expected? Am I using .ToList() correct? Or what else would solve my problem?
This is my code i have so far:
IList<Models.Browser.Language> languages = GetLanguages(dateDrom, dateTo).ToList();
IList<Models.Browser.Language> primaryItems = languages.Take(10);
This last line produces an error saying:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Bogus.Models.Browser.Language>' to 'System.Collections.Generic.IList<Bogus.Models.Browser.Language>'. An explicit conversion exists (are you missing a cast?)
Thanks in advance!
The value of
languagesis a reference to aList<>, but then you’re callingTake(10)on that, which doesn’t return a list.Just move your
ToList()call:Or just do it in one call:
Also, this isn’t quite correct:
An alternative is to use
Concat. For example: