C# Convert Typed list to Generics

Born2Achieve 21 Reputation points
2022-12-19T21:11:04.94+00:00

Hello,

I am using vs2022 and C# language and oracle as backend. oracle procedure gets input as facility type and based on type it returns the list of data and each facility type have common columns with few additional columns per type. on the below code, MaptoList is an extension class method which maps the reader column and value to class. the below logic is not working and says cannot convert from non generic list to generic. please do the needful to solve the compile error.

public class Common  
{  
  
        public string Name { get; set; }  
        public string Address { get; set; }  
        public string State{ get; set; }  
  
}  
  
  
 public class Employee : Common  
    {  
        public string SSN{ get; set; }  
    }  
  
public class Vendor : Common  
    {  
          
	public string TaxNumber{ get; set; }  
    }  
  
  
  
 public async Task<List<T>> GetFacility(string Facility_Type)  
        {  
            using (EMPDB dbConnect = new EMPDB())  
            {  
                DbCommand dbCommand = dbConnect.Database.GetDbConnection().CreateCommand();  
                dbCommand.CommandType = CommandType.StoredProcedure;  
                dbCommand.CommandText = "GetFacilityReport";  
                dbCommand.BuildSqlParameter("Facility_Type", OracleDbType.Varchar2, Facility_Type, ParameterDirection.Input);   
                List<T> lstFacility_Response= new List<T>();  
                if (dbCommand.Connection.State != ConnectionState.Open)  
                {  
                    dbCommand.Connection.Open();  
                }  
                using (var reader = await dbCommand.ExecuteReaderAsync())  
                {  
		   switch (Facility_Type)  
                    {	  
			case "Employee":  
			lstFacility_Response= reader.MapToList<Employee>();  
			break;  
  
			case "Vendor":  
			lstFacility_Response= reader.MapToList<Vendor>();  
			break;  
                      
		   }  
                }  
                 
            }  
 return lstFacility_Response  
        }  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-12-19T21:41:34.097+00:00

    Seem like you can do this which is slimmed down and using my classes GetFacility<Customers>();

    public static List<T> GetFacility<T>()  
    {  
        Type type = typeof(T);  
      
        if (type == typeof(Contacts))  
        {  
            Console.WriteLine("Do work for Contact");   
        }  
        else if (type == typeof(Customers))  
        {  
            Console.WriteLine("Do work for Customer");  
        }  
        else if (type == typeof(Products))  
        {  
            Console.WriteLine("Do work for Product");  
        }  
      
      
        return Array.Empty<T>().ToList();  
      
    }  
    

  2. Bruce (SqlWork.com) 81,976 Reputation points Volunteer Moderator
    2022-12-19T21:42:33.287+00:00

    just like your mapper, you should use the type rather string value:

        public async Task<List<T>> GetFacility<T>() where T: Common  
        {  
             ...  
      
               
             return reader.MapToList<T>();  
        }  
            
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.