c# - Store Data In a Generic List and use Linq -


model.cs contains data of user stored in list type have fetch students age more 25.

public class model {     public int age { get; set; }     public string name { get; set; }     public string sports { get; set; }           list<model> objmodels = new list<model>() {             new model { name = "manish", age = 27, sports = "cricket" },             new model { name = "rajan", age = 25, sports = "football" },             new model { name = "prashant", age = 25, sports = "kabaddi" },             new model { name = "garima", age = 24, sports = "ludo" },             new model { name = "neha", age = 25, sports = "carom" }         };     } 

modelcontroller.cs

public class modelcontroller : controller     {          // get: model         public actionresult index()         {             list<model> objmodel = new list<model>();             var query = models in objmodel select models;             foreach(var item in query.tolist())             {                 objmodel.add(                     new model                     {                         name = item.name.tostring(),                         age = int.parse(item.age.tostring()),                         sports = item.sports.tostring()                     });              }             return view(objmodel);         }     } 

but object empty because not fetching data model class.

hi have tried do:

 public class model     {         public int age { get; set; }         public string name { get; set; }         public string sports { get; set; }         public list<model> models         {                         {                 return objmodels;             }              set {                 model = value;             }          }          list<model> objmodels = new list<model>() {             new model { name = "manish", age = 27, sports = "cricket" },             new model { name = "rajan", age = 25, sports = "football" },             new model { name = "prashant", age = 25, sports = "kabaddi" },             new model { name = "garima", age = 24, sports = "ludo" },             new model { name = "neha", age = 25, sports = "carom" }         };     } 

and use in controller :

 list<model> newmodellist=new list<model>();  model objmodel = model;             var query = models in objmodel.models models.age<25 select models;             foreach (var item in query.tolist())             {                 newmodellist.add(                     new model                     {                         name = item.name.tostring(),                         age = int.parse(item.age.tostring()),                         sports = item.sports.tostring()                     });              }             return view(newmodellist); 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -