免责声明:可能是长篇
有很多这样的问题,我不太乐意加入这个包,但是, 我有一个包含多行的表,每个行都有效日期,我的SQL查询将根据生效日期获取最新记录
select * from TblTaxmasters
我正在使用此查询来获得所需的结果
select * FROM (select t1.id,t1.shortcode,t1.rev_no,t1.percentage,t1.effectivedate startdate, isnull(MIN(t2.effectivedate),getdate()+1) as enddate FROM tbltaxmaster t1 LEFT
OUTER JOIN tbltaxmaster t2 on t1.id=t2.id AND t1.effectivedate < t2.effectivedate
GROUP BY t1.id,t1.shortcode,t1.effectivedate,t1.rev_no,t1.percentage ) tx where '2014-05-01' BETWEEN tx.startdate AND tx.enddate
输出是
现在我正在尝试将此查询转换为vb.net winforms 4.0中的实体框架dbcontext
这是我试过的
Dim tomm = DateTime.Today.AddDays(1)
Dim mydate = DateTime.Today
Dim tax = (From t In (From t1 In tblTaxmasters
From t2 In tblTaxmasters
Where t1.id = t2.id And t1.effectivedate < t2.effectivedate
Select New With {.id = t1.id, .shortcode = t1.shortcode, .maxdate = t1.effectivedate, .mindate = If(t2.effectivedate.HasValue, t2.effectivedate, tomm), .percentage = t1.percentage, .revno = t1.rev_no})
Where mydate >= t.mindate And mydate <= t.maxdate Select New With {t.shortcode, t.id, t.percentage, t.revno}).ToList()
它告诉我
没有
没有结果,然后我用linqpad尝试了同样的事情,它显示查询执行成功,但没有结果。我检查了sql输出,这里是我得到的
-- Region Parameters
DECLARE @p0 DateTime = '2014-05-10 00:00:00.000'
DECLARE @p1 DateTime = '2014-05-09 00:00:00.000'
DECLARE @p2 DateTime = '2014-05-09 00:00:00.000'
-- EndRegion
SELECT [t2].[shortcode], [t2].[id], [t2].[percentage], [t2].[rev_no] AS [revno]
FROM (
SELECT [t0].[id], [t0].[shortcode], [t0].[effectivedate],
(CASE
WHEN [t1].[effectivedate] IS NOT NULL THEN [t1].[effectivedate]
ELSE @p0
END) AS [value], [t0].[percentage], [t0].[rev_no], [t1].[id] AS [id2], [t1].[effectivedate] AS [effectivedate2]
FROM [tblTaxmaster] AS [t0], [tblTaxmaster] AS [t1]
) AS [t2]
WHERE (@p1 >= [t2].[value]) AND (@p2 <= [t2].[effectivedate]) AND ([t2].[id] = [t2].[id2]) AND ([t2].[effectivedate] < [t2].[effectivedate2])
显然是某种东西
在翻译中丢失
所以任何人都可以指出我向前迈进的方向。
答案 0 :(得分:0)
@Mark,感谢您指出正确的方向,实际上我必须在另一个论坛的this链接的帮助下进行调整。但很高兴两者都成功了,所以这就是我最后所做的事情
Dim tax = From c In ctx.tblTaxmasters
Group c By c.id Into g = Group
Select g.OrderByDescending(Function(x) x.effectivedate).FirstOrDefault()
Dim mytax = tax.ToList()
感谢您的帮助
今天我偶然在msdn内搜索时,基于相同主题的大量问题已经可用,而最突出的答案来自堆栈溢出。它比我想象的更深入,它引导我对vb和c#编译器的匿名类型的关键关键字等的差异。一个令人惊讶的事实是,msdn搜索比google上点。