How to assign more than one Collection in a ObservableCollection using Inline C# Statement? -


i'm having collection of model

public class mobilemodelinfo {     public string name { get; set; }     public string catagory { get; set; }     public string year { get; set; } } 

here need assign morethan 1 collection within new observablecollection<mobilemodelinfo>() using single statement without using moblist.add() following statement

observablecollection<mobilemodelinfo> moblist = new observablecollection<mobilemodelinfo>(     (new mobilemodelinfo { name = "s4", catagory = "smart phone", year = "2011" }),     (new mobilemodelinfo { name = "s5", catagory = "smart phone", year = "2013" }),     (new mobilemodelinfo { name = "s6", catagory = "ultra smart phone", year = "2015" }) ); 

but generates compile time error, kindly assist me achieve via inline statement.

note: don't suggest moblist.add()

an observablecollection accepts parameters either list<t>, ienumerable<t> or nothing.

you forgot declare list before adding items. try this:

observablecollection<mobilemodelinfo> moblist = new observablecollection<mobilemodelinfo>(new list<mobilemodelinfo>(){     new mobilemodelinfo { name = "s4", catagory = "smart phone", year = "2011" },     new mobilemodelinfo { name = "s5", catagory = "smart phone", year = "2013" },     new mobilemodelinfo { name = "s6", catagory = "ultra smart phone", year = "2015" } }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -