Search code examples
c#dapper

Dapper returning null values for few objects


While mapping to the (queryasync)dapper few of the objects are returning null even though have value in database table.
Null is returning for PremiumCcy , ReportingClass2 , SyndEPI , SyndGrossEPI , SyndExposure , LimitCcyISO

I am really getting frustrated for solving this issue, already invested 3 days , any help is really appreciated over here.

My code :

  var query = @$"                   
                   SELECT 
    p.PolicyRef , 
    p.RenewedFromRef,
    p.PolicyId ,
    p.YOA as PolicyYOA,
p.PlacingType as PlacingType,
p.StatsCcyISO as PremiumCcy,
pl.SyndExposure,
pl.SyndEPI ,
pl.SyndGrossEPI,
    repClass.ReportingClass2,   
    plimit.LimitCcyISO, 
    lsp.LineSizePct as LineSizePct,
        (SELECT MAX(lastUpd) 
         FROM (
            VALUES (p.LastUpd), 
                (pl.LastUpd), 
                (plimit.LastUpd), 
                (repClass.LastUpd)
                ) AS VALUE(lastUpd)
        ) as LastUpd
FROM Policy p
    INNER JOIN PolicyLine pl
    ON p.PolicyId = pl.PolicyId
    LEFT JOIN ReportingClass repClass 
    ON p.YOA = repClass.PIMYear
    AND repClass.Synd = pl.Synd 
    AND repClass.ProducingTeam = pl.ProducingTeam
    AND repClass.Class1 = p.Class1
    AND repClass.Class2 = p.Class2
    AND repClass.Class3 = p.Class3
    AND repClass.Class4 = p.Class4 
    LEFT JOIN PolicyLimit plimit
    ON p.PolicyId = plimit.PolicyId
    AND plimit.TopLimitInd = 'Y'
    AND plimit.DelDate IS NULL
    AND plimit.Limit IS NOT NULL
WHERE pl.Synd = '1234'
    AND pl.LineStatus IN ('ABC', 'XYZ') 
        AND p.PlacingType <> 'DEC';
                    ;";



                    var eclipsePolicy = await connection.QueryAsync<EclipsePolicy, PolicyLines, ReportingClass, PolicyLimit, LineSize, EclipsePolicy>(query,
                        (eclipsePolicy, policyLines, reportingClass, policyLimit, lineSize) =>
                        {
                            eclipsePolicy.PolicyLines = policyLines;
                            eclipsePolicy.ReportingClasses = reportingClass;
                            eclipsePolicy.PolicyLimits = policyLimit;
                            eclipsePolicy.LineSizes = lineSize;
                            return eclipsePolicy;

                        }, CommandType.Text, connection.BeginTransaction(), splitOn: "PremiumCcy,SyndExposure,ReportingClass2,LimitCcyISO");

Model I am using is

public class EclipsePolicy
    {
        public string PolicyRef { get; set; }  

        public string RenewedFromRef { get; set; } 

        public int PolicyId { get; set; }  

        public int PolicyYOA { get; set; } 

        public string PlacingType { get; set; } 

        public DateTime EclipseLastUpd { get; set; }  , 
        
        public PolicyLimit PolicyLimits { get; set; }
        public string PremiumCcy { get; set; }  
        public ReportingClass ReportingClasses { get; set; }
        public PolicyLines PolicyLines { get; set; }
        public decimal Premium  { get; set; }
        public LineSize LineSizes { get; set; }

    }

   public class PolicyLines
   {
       public decimal SyndEPI { get; set; }  

       public decimal SyndGrossEPI { get; set; }
       public string SyndExposure { get; set; } 
    }

   public class ReportingClass
   {
       public string ReportingClass2 { get; set; }  
   }

   public class PolicyLimit
   {
       public string LimitCcyISO { get; set; } 
   }

   public class LineSize
   {
       public decimal LineSizePct { get; set; }
    }

Solution

  • Better to handle mapping manually to see what's the problem like the following:

          var result = await connection.QueryAsync<dynamic>(query);
    
    
                    return MapResult(result);
    
     private dynamic MapResult(dynamic result)
            {
                var eclipsePolicy = new EclipsePolicy
                {
                    EclipseLastUpd = result[0].EclipseLastUpd,
                    .
                    .
                    .
                    .
                    ReportingClasses = result[0].ReportingClasses
                };
                var eclipsePolicy = new PolicyLines
                {
                    SyndEPI = result[0].SyndEPI,
    
                  .
    
                  .
    
                  .
    
                  .
                  SyndGrossEPI = result[0].SyndGrossEPI
                };
    
    
                return new {eclipsePolicy,eclipsePolicy ,...};
            }