将 T-SQL 转换为 Linq
我是 LINQ 新手,被要求将我们现有的旧解决方案更新为 EF4.1 中的 Linq to Entities。
EF模型在这里 http://i53.tinypic.com/2h6sy0m.png
我可以管理基本查询,但是如下所示的扩展性问题正在引起严重的头痛。如果您在将其转换为 Linq 时提供任何建议,我将不胜感激(Return Me.ObjectContext.** ....)
SELECT tblInspectionFailures.ID, vwBusinessUnit.Name, vwBuilds.BuildNo, tblFaultArea.Description AS [Fault Area], tblFault.Description AS Fault,
tblFaultsCodes.Description AS [Fault Code], tblFaultsCodesDetail.Description AS [Fault Code Detail],
tblFaultCode_AuditScores.Description AS [Audit Score], tblInspectionFailures.Comment, tblInspectionFailures.ShortagePart,
tblInspectionFailures.CreatedDate, vwUsers.DisplayName AS [Created By], tblInspectionFailures.IsAdhocTest, tblInspectionFailures.FixedDate
FROM tblInspectionFailures INNER JOIN
tblFaultArea ON tblFaultArea.ID = tblInspectionFailures.FaultAreaID INNER JOIN
vwBuilds ON tblInspectionFailures.BuildID = vwBuilds.ID INNER JOIN
tblFaultsCodes ON tblInspectionFailures.FaultCodeID = tblFaultsCodes.ID INNER JOIN
vwBusinessUnit ON tblInspectionFailures.BUID = vwBusinessUnit.ID INNER JOIN
vwUsers ON tblInspectionFailures.CreatedBy = vwUsers.ID INNER JOIN
tblFault ON tblInspectionFailures.FaultID = tblFault.ID LEFT OUTER JOIN
tblFaultCode_AuditScores ON tblInspectionFailures.AuditScoreID = tblFaultCode_AuditScores.ID LEFT OUTER JOIN
tblFaultsCodesDetail ON tblInspectionFailures.FaultCodeDetailID = tblFaultsCodesDetail.ID
WHERE (tblInspectionFailures.FaultAreaID IN (28, 72, 78))
AND (tblInspectionFailures.CreatedDate BETWEEN CONVERT(DATETIME, '2011-04-01 00:00:00', 102) AND GETDATE())
I am new to LINQ and have been asked to update our existing legacy solution to Linq to Entities in EF4.1.
The EF model is here
http://i53.tinypic.com/2h6sy0m.png
I can manage with the basic queries but the expansive ones, such as below, are causing major headaches. I would appreciate any advice you would have to offer in converting this over to Linq (Return Me.ObjectContext.** ....)
SELECT tblInspectionFailures.ID, vwBusinessUnit.Name, vwBuilds.BuildNo, tblFaultArea.Description AS [Fault Area], tblFault.Description AS Fault,
tblFaultsCodes.Description AS [Fault Code], tblFaultsCodesDetail.Description AS [Fault Code Detail],
tblFaultCode_AuditScores.Description AS [Audit Score], tblInspectionFailures.Comment, tblInspectionFailures.ShortagePart,
tblInspectionFailures.CreatedDate, vwUsers.DisplayName AS [Created By], tblInspectionFailures.IsAdhocTest, tblInspectionFailures.FixedDate
FROM tblInspectionFailures INNER JOIN
tblFaultArea ON tblFaultArea.ID = tblInspectionFailures.FaultAreaID INNER JOIN
vwBuilds ON tblInspectionFailures.BuildID = vwBuilds.ID INNER JOIN
tblFaultsCodes ON tblInspectionFailures.FaultCodeID = tblFaultsCodes.ID INNER JOIN
vwBusinessUnit ON tblInspectionFailures.BUID = vwBusinessUnit.ID INNER JOIN
vwUsers ON tblInspectionFailures.CreatedBy = vwUsers.ID INNER JOIN
tblFault ON tblInspectionFailures.FaultID = tblFault.ID LEFT OUTER JOIN
tblFaultCode_AuditScores ON tblInspectionFailures.AuditScoreID = tblFaultCode_AuditScores.ID LEFT OUTER JOIN
tblFaultsCodesDetail ON tblInspectionFailures.FaultCodeDetailID = tblFaultsCodesDetail.ID
WHERE (tblInspectionFailures.FaultAreaID IN (28, 72, 78))
AND (tblInspectionFailures.CreatedDate BETWEEN CONVERT(DATETIME, '2011-04-01 00:00:00', 102) AND GETDATE())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SQL查询比较复杂,有很多内连接和外连接。这个答案没有考虑到这一切。然而,LINQ 提供程序(例如实体框架)的优点在于它们可以直接从模型暗示连接关系。我每天都会编写 LINQ 查询,有时它们会变得非常复杂,但我几乎不需要在 LINQ 查询中进行显式联接。当您拥有设计数据库模型和实体框架模型时,您可以简单地将所有内容“点”在一起,而无需使用 LINQ
join
运算符。说够了。您可以将以下查询作为起点。它从数据库中获取给定数据范围内和给定故障区域的所有故障。您可以扩展它以准确返回您需要的内容,但我希望这可以作为一个起点有所帮助。
祝你好运。
The SQL query is complicates and has a lot of inner joins and outer joins. This answer is not taking this all into consideration. However what's nice about LINQ providers (such as Entity Framework) is that they can imply the join relationships directly from the model. I write LINQ queries on a daily basis and they can get pretty complicated sometimes, but I hardly ever need to do explicit joining in my LINQ queries. When you have a design database model and Entity Framework model, you can simply 'dot' everything together without using the LINQ
join
operator.Enough talking. You the following query as starting point. It gets all the failures from the database within the given data range and for the given failt areas. You can extend it to return exactly what you need, but I hope this helps as a starting point.
Good luck.