Sql查询到EF Linq表达式

时间:2011-04-27 14:36:00

标签: sql linq entity-framework entity-framework-4 linq-to-entities

任何人都可以帮助这个SQL查询将其转换为EF Linq Expression吗?

SELECT Aitimata.id, AST.dateCr, AST.StatusId, Aitimata.UniqueCode, Aitimata.CategoryId, Aitimata.Thema
FROM         Aitimata INNER JOIN
    (select a.* from AitimataStatus a join
    (select AitimaId, max(dateCr) AS dateCr
    from AitimataStatus
    group by AitimaId) b
     on a.AitimaId = b.AitimaId and a.dateCr = b.dateCr)as AST ON Aitimata.id = AST.AitimaId

重要的是这个子查询

select a.* from AitimataStatus a join
(select AitimaId, max(dateCr) AS dateCr
from AitimataStatus
group by AitimaId) b
on a.AitimaId = b.AitimaId and a.dateCr = b.dateCr

您需要额外的信息吗?

由于

2 个答案:

答案 0 :(得分:0)

这将是这样的:

from a in AitimataStatus 
let b = from  as in AitimataStatus group by and other stuff and then select
join b on a.** equals b.**

答案 1 :(得分:0)

这是我的解决方案

Dim sub1 = From aitst In context.AitimataStatus
                Join aitst2 In (From aitst2 In context.AitimataStatus
                                     Group aitst2 By cid = aitst2.AitimaId Into g = Group
                                     Select New With {.AitimaId = cid, .dateCr = g.Max(Function(ddd) (ddd.dateCr))}
                                     )
                On aitst2.AitimaId Equals aitst.AitimaId And aitst2.dateCr Equals aitst.dateCr
                Select aitst

它产生

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[dateCr] AS [dateCr], 
[Extent1].[UserId] AS [UserId], 
[Extent1].[AitimaId] AS [AitimaId], 
[Extent1].[StatusId] AS [StatusId]
FROM  [dbo].[AitimataStatus] AS [Extent1]
INNER JOIN  (SELECT 
    [Extent2].[AitimaId] AS [K1], 
    MAX([Extent2].[dateCr]) AS [A1]
    FROM [dbo].[AitimataStatus] AS [Extent2]
    GROUP BY [Extent2].[AitimaId] ) AS [GroupBy1] ON (([Extent1].[AitimaId] = [GroupBy1].[K1]) OR (([Extent1].[AitimaId] IS NULL) AND ([GroupBy1].[K1] IS NULL))) AND (([Extent1].[dateCr] = [GroupBy1].[A1]) OR (([Extent1].[dateCr] IS NULL) AND ([GroupBy1].[A1] IS NULL)))