c# - How to store and sort two types of classes in List<>? -


i have 2 classes:

public class savedquote {     public int id { get; set; }     public string text { get; set; }     public string context { get; set; }     public string url { get; set; }     public string comment { get; set; }     public string wheretosearch { get; set; }      public datetime dateofadding { get; set; }      public string ownername { get; set; }     public virtual icollection<tag> tags { get; set; } }  public class noteonsite {     public int id { get; set; }     public string url { get; set; }     public datetime dateofadding { get; set; }     public string ownername { get; set; }     public string comment { get; set; }      public virtual icollection<tag> tags { get; set; } } 

i have 2 lists: 1 represents "savedquotes" , 1 has "noteonsites". need sort data lists dateofadding , display them in 1 table on webiste.

the problem is: (probably) can't save objects 2 different classes in 1 list<> (i need sort objects). advise me do? how solve problem?

i (probably) can't save objects 2 different classes in 1 list<>

you can, long object have common base class. in c#, objects have common base class system.object, enough store objects of entirely different types in single list.

a heavyweight approach put common interface on objects wish sort:

public interface iwithdate {     public datetime dateofadding { get; set; } } public class savedquote : iwithdate {     ... } public class noteonsite : iwithdate {     ... } ... var mixedlist = new list<iwithdate>(); 

however, may introduce more structure wish: making classes related each other through common interface much, if need sort objects of these classes together.

if wish sort objects on commonly named property without adding static structure around classes, can make list of dynamic objects, , use dateofadding directly:

var mixedlist = new list<dynamic>(); mixedlist.addrange(quotes); mixedlist.addrange(notes); mixedlist.sort((a, b)=>a.dateofadding.compareto(b.dateofadding)); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -