Question

I have a problem where im trying to add my items to my observable collection, but i get this error:

Argument 1: cannot convert from 'System.Collections.Generic.List' to 'GameFinder.GetGamesList'

code:

private void RequestCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        if (e.Error == null)
        {
            var feedXml = XDocument.Parse(e.Result);

            var gameData = feedXml.Root.Elements("Game").Select(x => new GetGamesList
            {
                ID = (int)x.Element("id"),
                GameTitle = (string)x.Element("GameTitle"),
                ReleaseDate = (string)x.Element("ReleaseDate"),
                Platform = (string)x.Element("Platform")
            })
              .ToList();
            Items.Add(gameData); // THE ERROR IS HERE - Items is the observablecollection
        }
    }

private ObservableCollection<GetGamesList> _Items = new ObservableCollection<GetGamesList>();
    public ObservableCollection<GetGamesList> Items
    {
        get
        {
            return this._Items;
        }
    }

public class GetGamesList
{
    public int ID { get; set; }
    public string GameTitle { get; set; }
    public string ReleaseDate { get; set; }
    public string Platform { get; set; }
}
Was it helpful?

Solution

Try

foreach (var item in gameData) Items.Add(item)

gameData is a List<GetGamesList> so you need to add each item from gameData into Items list and since ObservableCollection doesn't have AddRange you'll need to do it manually in the loop

OTHER TIPS

What exactly don't you understand on cannot convert from 'System.Collections.Generic.List' to 'GameFinder.GetGamesList'? Add method accepts single element, whereas you are passing in a list of elements. You can use new ObservableCollection<GetGamesList>(gameData). And remove the ToList statement at the end of you Linq query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top