Guillermo Perez

Guillermo Perez

  • NA
  • 27
  • 398

Generic Local Function and List

Sep 12 2020 4:20 PM
Thank you for reading! I'm getting a list of objects (from a dbcontext), then I created a method to pass this list to a Local Function, the method accepts the list but when I try to loop, instead of being able to show the content of my object members I get an error saying: "'T' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?) " And this problem is not only with Id, I can't access any of the properties, please check the code: would you please help me find what I am missing? thank you for your advice.
  1. public IActionResult Racks()  
  2.         {  
  3.   
  4.             List<OldRackAustin> mylist = db.OldRacksAustin.ToList<OldRackAustin>();  
  5.   
  6.             ProcessMyList(mylist);  
  7.   
  8.             void ProcessMyList<T>( List<T> list)  
  9.             {  
  10.   
  11.                 foreach (T item in list)  
  12.                 {  
  13.                     Debug.WriteLine(item.Id);  /* PROBLEMATIC LINE */
  14.                 }  
  15.   
  16.             }  
  17.   
  18.             return Json("done");  
  19.         }  
 And the DBcontext entity (object) looks like...
 
  1. [Table("OLD-Racks-Austin")]  
  2.     public class OldRackAustin   
  3.     {  
  4.         [Key]  
  5.         public int Id { getset; }  
  6.         public string Rack_Type { getset; }  
  7.         public string Serial_Number { getset; }  
  8.         public short Pages { getset; }  
  9.         public int? Rack_Capacity { getset; }  
  10.         public string Status { getset; }  
  11.         public string Type { getset; }  
  12.         public string Subtype { getset; }  
  13.   
  14. }  

Answers (1)