【问题标题】:Subqueries in dapper no longer workingdapper 中的子查询不再起作用
【发布时间】:2014-10-23 17:06:07
【问题描述】:

我正在使用 dapper,我刚刚实现了 splitOn 函数来获取在我的 AdListingSearchResult 中设置的位置对象,但是这样做我的子查询(MainPhotoFileName 和 TotalPhotos)现在分别为 null 和 0。知道我在这里做错了什么吗?

var data = GetConnection().Query<AdListingSearchResult, AdListingLocationSearchResult, AdListingSearchResult>(@"
SELECT TOP 10 a.AdListingID, a.Title, a.Details, a.CreateDateTime, l.LocationID, l.CountryID, l.USCity, l.USStateCode, l.IntlRegion, c.CountryName,  
(SELECT TOP 1 ap.Filename FROM tbAdListingPhotos ap WHERE ap.AdListingID = a.AdListingID) AS MainPhotoFileName, 
(SELECT COUNT (*) FROM tbAdListingPhotos ap WHERE ap.AdListingID = a.AdListingID) AS TotalPhotos
FROM tbAdListing a 
INNER JOIN tbLocation l ON (a.LocationID = l.LocationID) 
INNER JOIN tbEnumCountry c ON (l.CountryID = c.CountryID) 
WHERE a.Deleted = 0  ORDER BY a.CreateDateTime DESC                 
", (a, l) =>
 {
     a.Location = l;
     return a;
 },
 splitOn: "LocationId"
 ).AsQueryable();

return data;

【问题讨论】:

  • 哪个对象具有MainPhotoFileName (etc) 属性? AdListingSearchResult?或AdListingLocationSearchResult?

标签: c# dapper


【解决方案1】:

我猜这些子查询是为了映射到AdListingSearchResult 对象的属性。但是,该行是根据splitOn 参数水平分区的,这意味着AdListingSearchResult 永远不会看到这些值。

您应该能够简单地重新排序 SQL:

SELECT TOP 10 a.AdListingID, a.Title, a.Details, a.CreateDateTime,
(SELECT TOP 1 ap.Filename FROM tbAdListingPhotos ap WHERE ap.AdListingID = a.AdListingID) AS MainPhotoFileName, 
(SELECT COUNT (*) FROM tbAdListingPhotos ap WHERE ap.AdListingID = a.AdListingID) AS TotalPhotos,
l.LocationID, l.CountryID, l.USCity, l.USStateCode, l.IntlRegion, c.CountryName
FROM tbAdListing a
...

【讨论】:

  • 我今天确实在考虑这个问题。我会试一试并很快回来报告。谢谢!
  • 就是这样,非常感谢。我只需要重新排序我的选择语句。
猜你喜欢
  • 1970-01-01
  • 2013-09-04
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多