简体   繁体   English

无法将SQL查询转换为实体框架vb.net

[英]unable to convert SQL query to entity framework vb.net

Disclaimer: may be long post 免责声明:可能会发布很长时间

There is a lot of questions like this, and I am not quite happy to join the pack but yet , I am having a table which contains several rows and each they have effective dates, my sql query will fetch the latest records based on the effective date 像这样有很多问题,我不太乐意加入这个包,但是,我有一个表,其中包含几行,每行都有有效日期,我的sql查询将根据有效行获取最新记录日期

select * from TblTaxmasters

在此处输入图片说明

i am using this query to get desired result 我正在使用此查询以获得所需的结果

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 

The output is 输出是 在此处输入图片说明

now i am trying to convert this query against entity framework dbcontext in vb.net winforms 4.0 现在我正在尝试针对vb.net winforms 4.0中的实体框架dbcontext转换此查询

this is what i tried 这就是我尝试过的

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()

and it told me 它告诉我

nothing 没有

no result , then i tried the same thing with linqpad and it shows query executed successfully but no results. 没有结果,然后我用linqpad尝试了同样的事情,它显示查询已成功执行,但没有结果。 I checked the sql output and here what i get 我检查了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])

obviously something 显然是

lost in translation 迷失在翻译中

so anybody can point me which way to move forward. 这样任何人都可以指出我前进的方向。

@Mark, Thanks for pointing the right direction, actually i have to tweak it with the help of this link in another forum. @Mark,感谢您指出正确的方向,实际上我必须在另一个论坛中的链接的帮助下进行调整。 But glad both worked out, so here is what i have did finally 但是很高兴两个人都解决了,所以这就是我最后所做的

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()

Thanks for the help 谢谢您的帮助

Today accidentally when i searched inside msdn there are loads of questions based on the same topic is already available, and most prominent answers are from stack over flow. 今天,不经意间,当我在msdn中进行搜索时,已经有大量基于同一主题的问题可用,而最突出的答案来自堆栈溢出。 And it digs deeper than what i thought , it leads me to vb and c# compiler difference of anonymous types the key keyword etc. one surprising fact is that msdn search is spot on than google. 而且它比我想的要深入,它使我发现匿名类型的vb和c#编译器的区别,关键关键字等。一个令人惊讶的事实是,msdn搜索比google更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM