Home > Mobile >  how to return result in net core with dapper
how to return result in net core with dapper

Time:09-06

Hello i am dummy and i can not understend why i can not return result to entities and get error message Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'ReportingApp.Entities.Reports.Reporting.GetInfoForUserSummary'. An explicit conversion exists (are you missing a cast?). The res variable is IEnumerable and i also have four entities that are in one group class summary.

 public class InfozasitePcja
    {
        public string ComputerName { get; set; }
        public string Domain { get; set; }
        public string User { get; set; }
        public string BiosSerial { get; set; }
        public string SystemEnclosureSerial { get; set; }
        public string Manufacturer { get; set; }
        public string model { get; set; }
        public string OS { get; set; }
        public string Site { get; set; }
        public int TotalMemory { get; set; }
        public int HardriveSize { get; set; }
        public int FreeSpace { get; set; }
        public int MaxCPUSpeed { get; set; }
        public string CPUModel { get; set; }
        public int ID64BitCompatible { get; set; }

    }
    [Table("SEPreport")]
    public class SEPreport
    {
        public string COMPUTER_NAME { get; set; }
        public string PROCESSOR_TYPE { get; set; }
        public string PROCESSOR_CLOCK { get; set; }
        public string PROCESSOR_NUM { get; set; }
        public string MEMORY { get; set; }
        public string BIOS_VERSION { get; set; }
        public string OPERATION_SYSTEM { get; set; }
        public string SERVICE_PACK { get; set; }
        public string CURRENT_LOGIN_USER { get; set; }
        public string MAC_ADDR1 { get; set; }
        public string IP_ADDR1 { get; set; }
        public string DISK_TOTAL { get; set; }
        public string BIOS_SERIALNUMBER { get; set; }
        public string AGENT_VERSION { get; set; }
        public string STATUS { get; set; }
        public string LAST_UPDATE_TIME { get; set; }
    }
    [Table("Users")]
    public class Users
    {
        public string displayname { get; set; }
        public string SamAccountName { get; set; }
        public int EmployeeNumber { get; set; }
        public DateTime whenCreated { get; set; }
        public string distinquishedName { get; set; }
        public string Company { get; set; }
        public int OfficePhone { get; set; }
        public string mail { get; set; }
        public string distinguishedname { get; set; }
        public DateTime LastLogonDate { get; set; }
    }
  
    public class Eerv
    {
       
        public int Kadrov { get; set; }
        public string Username { get; set; }       
        public string Ime { get; set; }        
        public string Prezime { get; set; }
      
        public float TMEERV { get; set; }
 
        public string NazivEERV { get; set; }
  
        public string TMSAP { get; set; }
        [Column("Naziv TM SAP")]
        public string NazivESAP { get; set; }
    } 

Summary class

public partial class GetInfoForUserSummary
    {
        public IEnumerable<InfozasitePcja> InfozasitePcja { get; set; }
        public IEnumerable<SEPreport> SEPreport { get; set; }
        public IEnumerable<Users> Users { get;set; }    
        public IEnumerable<Eerv> Eerv { get;set;}
    }

Code

public async Task<GetInfoForUserSummary> GetInfoForUser(string column, string search)
        {
            //GetInfoForUserSummary result = new GetInfoForUserSummary();
            try
            {
                var query = "Select i.*,s.*,u.*,e.* From InfoZaSitePCja i "  
                    "INNER JOIN SEPreport s ON i.ComputerName = s.COMPUTER_NAME "  
                    "INNER JOIN Users u ON s.CURRENT_LOGIN_USER = u.SamAccountName "  
                    "INNER JOIN Eerv e ON u.EmployeeNumber = e.[kadr.br.] "  
                    "Where ["   column   "] LIKE '%'   @search   '%'";
                var param = new DynamicParameters();
                param.Add("@search", search, DbType.String);
                var res = await Connection.QueryAsync(query, param, commandType: CommandType.Text, transaction: Transaction);
                return res;
                //result.InfozasitePcja = await res.ToList<InfozasitePcja>();// .ReadAsync<InfozasitePcja>();
                //result.SEPreport = await res.ReadAsync<SEPreport>();
                //result.Users = await res.ReadAsync<Users>();
                //result.Eerv = await res.ReadAsync<Eerv>();
            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw new Exception("Database Error", e);
            }
            //return result;
        }

CodePudding user response:

1 - Type of res is an IEnumerable and you return IEnumerable to GetInfoForUserSummary. that is wrong. you should return an object of type GetInfoForUserSummary instead of IEnumerable.

2 - You should split your query result into GetInfoForUserSummary model. see these links to split result to your model 1 , 2

CodePudding user response:

I think your problem is that QueryAsync returns a dataset and not a single object. Also you're not using the generic variant so it won't cast the data to your required type.

Try using Connection.QueryFirstOrDefaultAsync<GetInfoForUserSummary>(query, param, commandType: CommandType.Text, transaction: Transaction) instead. See documentation.

  • Related