Dapper and DateTime in WHERE clause causes multi-mapped nulls

dapper datetime sql-server

Question

I finally was able to construct a multi-mapped query and return meaningful data. This query returned a list of a custom object which itself is composed of some other objects. But this query worked with a single parameter.

When I modified this query by adding a second parameter, a DateTime, two of the aggregated objects (Part and Color) were null. However, when I captured the SQL in the profiler and ran it in SQL Server, all of the data was there!

How can I better deal with a DateTime parameter in the where clause? It is obviously this which is causing the problem.

The code that follows works with the commented-out where clause but not the existing one.

  public IList<PartReceipt> GetReceiptHistory(ListItem supplier, DateTime dateReceived)
  {
     const string sql =
        @"SELECT r.id,  r.Quantity, r.UnitCost, r.DateReceived,
                 s.Id , s.Description, 
                 p.Id, p.Number, p.Description, p.StockClass,
                 mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
                 m.Id , m.Description, m.ShortDescription, 
                 c.Id, c.Description, 
                 pc.Id, pc.Name, pc.Description
          FROM   PartReceipt r
          INNER JOIN Supplier s ON r.SupplierId = s.Id
          INNER JOIN Part p on r.PartId = p.Id
          INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
          INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
          LEFT JOIN Color c ON p.ColorId=c.Id
          LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
          WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";

     //           WHERE s.Id=@supplierId";
     IList<PartReceipt> reportData;
     using (DbConnection connection = ConnectionFactory.GetOpenConnection())
     {
        reportData = connection.Query<PartReceipt>(sql,
           new
           {
              supplierId = supplier.Id,
              dateReceived
           }).ToList();
     }
     return reportData;
  }  

And the supporting classes are:

  public class PartReceipt
  {
     public int Id { get; set; }
     public Supplier Supplier { get; set; }
     public Part Part { get; set; }
     public DateTime DateReceived { get; set; }
     public Decimal UnitCost { get; set; }
     public int Quantity { get; set; }
  }   

  public class Part
  {
     public Color Color { get; set; }
     public string Description { get; set; }
     public int Id { get; set; }
     public ManufacturerPart ManufacturerPart { get; set; }
     public string Number { get; set; }
     public string PicturePath { get; set; }
     public ProductCategory ProductCategory { get; set; }
     public string StockClass { get; set; }
  }

  public class ManufacturerPart
  {
     public string Description { get; set; }
     public int Id { get; set; }
     public int ManufacturerId { get; set; }
     public string Number { get; set; }
     public Manufacturer Parent { get; set; }
  }   

  public class Manufacturer
  {
     public string Description { get; set; }
     public int Id { get; set; }
     public string ShortDescription { get; set; }
  }   

  public class ProductCategory
  {
     public string Description { get; set; }
     public int Id { get; set; }
     public string Name { get; set; }
  }   

  public class Color
  {
     public string Description { get; set; }
     public int Id { get; set; }
  }  

Accepted Answer

I am sorry I was not more careful before posting this question. I did not construct the multi-mapped query properly. When I do, its works.

  public IList<PartReceipt> GetReceiptPart(ListItem supplier, DateTime dateReceived)
  {
     const string sql =
        @"SELECT r.id,  r.Quantity, r.UnitCost, r.DateReceived,
                 s.Id , s.Description, 
                 p.Id, p.Number, p.Description, p.StockClass,
                 mp.Id , mp.Number, mp.ManufacturerId, mp.Description,
                 m.Id , m.Description, m.ShortDescription, 
                 c.Id, c.Description, 
                 pc.Id, pc.Name, pc.Description
          FROM   PartReceipt r
          INNER JOIN Supplier s ON r.SupplierId = s.Id
          INNER JOIN Part p on r.PartId = p.Id
          INNER JOIN ManufacturerPart mp ON p.ManufacturerPartId=mp.Id
          INNER JOIN Manufacturer m ON mp.ManufacturerId = m.Id
          LEFT JOIN Color c ON p.ColorId=c.Id
          LEFT JOIN ProductCategory pc ON p.ProductCategoryId=pc.Id
          WHERE s.Id=@supplierId AND r.DateReceived = @dateReceived";

     IList<PartReceipt> reportData;
     using (DbConnection connection = ConnectionFactory.GetOpenConnection())
     {
        reportData =
         connection
            .Query
            <PartReceipt, Supplier, Part, ManufacturerPart, Manufacturer, Color, ProductCategory, PartReceipt>(
               sql,
               (receipt, supp, part, mfgPart, mfg, color, productCategory) =>
               {
                  receipt.Supplier = supp;
                  receipt.Part = part;
                  receipt.Part.ManufacturerPart = mfgPart;
                  receipt.Part.ManufacturerPart.Parent = mfg;
                  receipt.Part.Color = color;
                  receipt.Part.ProductCategory = productCategory;
                  return receipt;
               }, new { supplierId = supplier.Id, dateReceived })
            .ToList();
     }
     return reportData;
  }



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why