Eugene
Eugene

Reputation: 231

Pass GenericList to Observable Collection

I have Generic List

GetAllSponsor sponsorlist = new GetAllSponsor();
sponsorlist.getallsponsor();
string spnsrlst = sponsorlist.ToFormattedJsonString();

Sponsors sponsorObjectData = JsonConvert.DeserializeObject<Sponsors>(spnsrlst);

Here is Sponsors Class definition looks like:

public class Sponsors
        {
            public string id { get; set; }
            public string company_name { get; set; }
            public string description { get; set; }
            public string sponsor_level { get; set; }
            public string picture { get; set; }
            public string location { get; set; }
            public string website_url { get; set; }
            public string sm_twitter { get; set; }
            public string sm_facebook { get; set; }
            public string sm_linkedin { get; set; }
            public string sm_pinterest { get; set; }
            public string contact_number { get; set; }
            public string attachments { get; set; }
            public string date_time { get; set; }


        }

I need to pass it to Observable Collection.

How I can do this?

Upvotes: 0

Views: 104

Answers (2)

Dave Smash
Dave Smash

Reputation: 3001

To summarize our chat, I would move the download code out of that class. The class should be called Sponsor (since it represents a single sponsor), and it should just hold public properties for the data fields that you are receiving (ID, company_name, description, etc.) The JSON that you are getting is actually an array of Sponsors, so you need to deserialize it to a List<Sponsor> or something appropriate. Then, loop through your list, and add each sponsor to the ObservableCollection like this:

foreach (Sponsor s in mySponsors) 
{
   myCollection.Add(s);
}

Upvotes: 0

Jason
Jason

Reputation: 89129

List<SomeType> list = new List<SomeType>();

// add items to list 

ObservableCollection<SomeType> collection = new ObservableCollection<SomeType>(list);

Upvotes: 1

Related Questions