C# 将子查询值映射到属性
我用它来获取和映射数据。我有以下查询和相关类C# 将子查询值映射到属性,c#,sql-server,dapper,C#,Sql Server,Dapper,我用它来获取和映射数据。我有以下查询和相关类 SELECT pinv.* , sp.SupplierId,sp.SupplierName,sp.Contact1,sp.SupplierAddress,ac.AccountId,ac.AccountName, (Select Count(*) from ReturnInvoices rinv Where rinv.InvoiceId = pinv.PurchaseInvoiceId) as ReturnInvoicesCount FROM P
SELECT pinv.* , sp.SupplierId,sp.SupplierName,sp.Contact1,sp.SupplierAddress,ac.AccountId,ac.AccountName,
(Select Count(*) from ReturnInvoices rinv Where rinv.InvoiceId = pinv.PurchaseInvoiceId) as ReturnInvoicesCount
FROM PurchaseInvoices pinv
LEFT JOIN Suppliers sp ON sp.SupplierId = pinv.SupplierId
LEFT JOIN Accounts ac on ac.AccountId = pinv.AccountId
还有这个班
public class PurchaseInvoice
{
public int PurchaseInvoiceId { get; set; }
public int PurchaseOrderId { get; set; }
public string PurchaseInvoiceType { get; set; }
public int SupplierId { get; set; }
public int AccountId { get; set; }
public int ReceiptNumber { get; set; }
public int ReturnInvoicesCount { get; set; } // to map ReturnInvoiceCount by SubQuery
public DateTime PromisedDate { get; set; }
public DateTime InvoiceDate { get; set; }
public decimal InvoiceQuantity { get; set; }
public decimal Discount { get; set; }
public decimal Tax { get; set; }
public decimal ShippingCharges { get; set; }
public decimal Surcharge { get; set; }
public string PaymentMode { get; set; }
public decimal SubTotal { get; set; }
public decimal TotalPayment { get; set; }
public decimal AmountPaid { get; set; }
public decimal AmountRemaining { get; set; }
public string Comments { get; set; }
public string Status { get; set; }
public DateTime UpdatedDate { get; set; }
public Supplier Supplier { get; set; }
public ProductReceived ProductReceived { get; set; }
public Account Account { get; set; }
}
我不分享其他课程,因为它们不相关。现在,若我运行上面的查询,我会得到完美的结果。获取结果后,dapper返回的所有数据都很好,但是没有映射到ReturnInvoicesCount(或者idk其他问题)。这就是我使用整洁的方式
using (SqlConnection connection = new SqlConnection(_conString))
{
var query = QueryHelper.GetPurchaseInvoicesQuery(start, end, simpleSearchText, searchTokensList);
// ignore above line, just getting query with parameters etc
var data = await connection.QueryAsync<PurchaseInvoice, Supplier, Account>(query.Query,
query.queryParams, splitOn: "SupplierId,AccountId");
return data;
}
使用(SqlConnection=newsqlconnection(\u conString))
{
var query=QueryHelper.GetPurchaseInvoicesQuery(开始、结束、simpleSearchText、searchTokensList);
//忽略上面的行,只获取带有参数的查询等
var data=await connection.QueryAsync(query.query,
query.queryParams,splitOn:“供应商ID,账户ID”);
返回数据;
}
正如我所说,我得到了正确的结果映射,除了字段returninvoicesont
为0。在服务器上时,可以看到ReturnInvoiceCount
有一个值
您在
供应商ID
(映射到供应商
)和账户ID
(映射到账户
)上有拆分。问题在于returninvoicesont
位于要正确映射的错误列位置。目前,您告诉dapper,ReturnInvoiceScont属于帐户
由于returninvoicescont
属于PurchaseInvoice
您必须将其移动到SupplierId
之前。非常感谢你亚历克斯:)这正是我错过的。现在已经解决了。守福