locked
Help with generic method and list... RRS feed

  • Question

  • User-121299044 posted

    Thank you for reading! I'm getting a list of objects (from by dbcontext), then I created a method to pass this list to a Local Functionj, the method accept 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 advise.

    public class SampleClass
        {
    
            private AppDbContext db;
            public ImporterController(AppDbContext Db) => this.db = Db;
            public IActionResult Index()
            {
    
                List<OldRackAustin> mylist = db.OldRacksAustin.ToList<OldRackAustin>();
    
                ProcessMyList(mylist);
    
                void ProcessMyList<T>( List<T> list)
                {
    
                    foreach (T item in list)
                    {
                        Debug.WriteLine(item.Id);
                    }
    
                }
                return Json("done");
            }
        }

    And the DBcontext object looks like... but I think its not important at this time, anyway I'm posting it here...

    [Table("OLD-Racks-Austin")]
        public class OldRackAustin 
        {
            [Key]
            public int Id { get; set; }
            public string Rack_Type { get; set; }
            public string Serial_Number { get; set; }
            public short Pages { get; set; }
            public int? Rack_Capacity { get; set; }
            public string Status { get; set; }
            public string Type { get; set; }
            public string Subtype { get; set; }
            
            [Column("16 Pages")]
            public int? _16Pages { get; set; }
    
            [Column("20 Pages")] 
            public int? _20Pages { get; set; }
    
            [Column("24 Pages")] 
            public int? _24Pages { get; set; }
    
            [Column("28 Pages")] 
            public int? _28Pages { get; set; }
    
            [Column("32 Pages")] 
            public int? _32Pages { get; set; }
            
            [Column("36 Pages")] 
            public int? _36Pages { get; set; }
    
            [Column("40 Pages")] 
            public int? _40Pages { get; set; }
    
            [Column("44 Pages")] 
            public int? _44Pages { get; set; }
    
            [Column("48 Pages")] 
            public int? _48Pages { get; set; }
    
            [Column("52 Pages")] 
            public int? _52Pages { get; set; }
            
            [Column("56 Pages")] 
            public int? _56Pages { get; set; }
            
            [Column("60 Pages")] 
            public int? _60Pages { get; set; }
            
            [Column("64 Pages")] 
            public int? _64Pages { get; set; }
            
            [Column("68 Pages")] 
            public int? _68Pages { get; set; }
        }

    Thanks for all your help!

    Saturday, September 12, 2020 8:43 PM

All replies

  • User-474980206 posted

    Because there is no type constraint on T, to the method it’s just an object and has knowledge of any props not defined by the Object class.

    Sunday, September 13, 2020 4:24 PM
  • User585649674 posted

    change the line from

    void ProcessMyList<T>(List<T> list)

    change to

    void ProcessMyList<T>(List<T> list) Where T : OldRackAustin

    Or you could create a base class for all the context object.

    public class OldRackAustin : BaseDBContext <int> {
    }
    
    public class BaseDBContext <TEntity>{
    public TEntity id{get;set;}
    public datetime CreatedDate{get;set;}
    public datetime UpdatedDate{get;set;}
    }
    
    void ProcessMyList<T>(List<T> list) Where T:BaseDBContext 

    Monday, September 14, 2020 8:01 AM