2015-04-03 46 views
0

我终于能够构造一个多映射查询并返回有意义的数据。该查询返回了一个自定义对象的列表,该自定义对象本身由一些其他对象组成。但是这个查询使用一个参数。WHERE子句中的Dapper和DateTime会导致多重映射的空值

当我通过添加第二个参数DateTime修改此查询时,两个聚合对象(零件和颜色)为空。但是,当我在探查器中捕获SQL并在SQL Server中运行它时,所有数据都在那里!

如何更好地处理where子句中的DateTime参数?显然这是导致问题的原因。

下面的代码使用注释掉的where子句,但不是现有的子代码。

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 [email protected] AND r.DateReceived = @dateReceived"; 

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

而且支持类是:

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; } 
    } 
+0

我很惊讶这个作品。如何精确地知道7个id和5个描述中的哪一个映射到哪里。猜猜它回头看表名和别名然后 – Mackan 2015-04-03 22:09:45

+0

我也很惊讶。但是,如果我像我一样按对象排序SQL查询,它就会起作用。如果我把订单混合起来,结果很糟糕。例如,“颜色描述”可能已转到“零件描述”。 – user2443507 2015-04-03 22:43:07

+0

我认为这可能是由于,例如,结果集中'Color.Id'为空。然后'Part.Color'将为空,然后'PartReceipt.Part'变为null。一连串不可映射的对象。但我不确定。如果您想要将问题添加到该问题,则查询的输出可能会提示您。 – Mackan 2015-04-04 10:48:19

回答

0

我很抱歉,我没有张贴此问题之前更加谨慎。我没有正确构建多映射查询。当我做,它的作品。

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 [email protected] 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; 
    }