我正在将一些代码从 ColdFusion 迁移到 .NET LinqToSQL。以下查询当前嵌入到 ColdFusion 中。我正在考虑编写一个存储过程,而不是试图让 LinqToSQL 做到这一点。注意:在这种情况下,CF 中的 ## 实际上是 SQL 中的 #。我没有创建全局临时表
<cfquery name="qryTrafficDetails">
DECLARE @startDate date
DECLARE @endDate date
SET @startDate = <cfqueryparam cfsqltype="CF_SQL_date" value="#filter.StartDate#">
SET @endDate = <cfqueryparam cfsqltype="CF_SQL_date" value="#filter.endDate#">
SELECT CONVERT(date, CreateDate) AS CreateDate, url_vars.value('(search)[1]', 'nvarchar(max)') AS Item, remote_addr
INTO ##tempTraffic
FROM dbo.Traffic WITH (NOLOCK)
WHERE Createdate BETWEEN @startDate AND @endDate
AND url_vars.exist('.[search]') = 1
SELECT Area, AreaSort, Item, CalendarDate, isNull(Visitor, 0) AS Visitor, ISNULL(Hit,0) AS Hit
FROM dbo.Calendar LEFT JOIN
(
SELECT AreaSort = 10, 'Search' AS Area, FullGrid.Item AS Item, FullGrid.CalendarDate AS CreateDate,
COUNT(DISTINCT remote_addr) AS visitor,
COUNT(remote_addr) as hit
FROM (
SELECT DISTINCT CalendarDate, Item
FROM dbo.Calendar, ##TempTraffic
WHERE CalendarDate BETWEEN @startDate AND @endDate
) FullGrid
LEFT JOIN ##TempTraffic AS DataItem
ON FullGrid.CalendarDate = CreateDate
AND FullGrid.Item = DataItem.Item
GROUP BY FullGrid.Item, FullGrid.CalendarDate
) AS SummaryDetail
ON CalendarDate = SummaryDetail.CreateDate
WHERE CalendarDate BETWEEN @startDate AND @endDate
AND AreaSort IS NOT NULL
ORDER BY AreaSort, Item, CalendarDate
</cfquery>
<cfreturn qryTrafficDetails>
我关心的一些问题是:
- 如何处理临时表
- LinqToSql 是否涵盖了所有内容?
- 这是 LinqToSql 不合适的一个例子吗?
- 表现