将 T-SQL 转换为 Linq

发布于 2024-11-02 01:29:20 字数 1944 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2024-11-09 01:29:20

SQL查询比较复杂,有很多内连接和外连接。这个答案没有考虑到这一切。然而,LINQ 提供程序(例如实体框架)的优点在于它们可以直接从模型暗示连接关系。我每天都会编写 LINQ 查询,有时它们会变得非常复杂,但我几乎不需要在 LINQ 查询中进行显式联接。当您拥有设计数据库模型和实体框架模型时,您可以简单地将所有内容“点”在一起,而无需使用 LINQ join 运算符。

说够了。您可以将以下查询作为起点。它从数据库中获取给定数据范围内和给定故障区域的所有故障。您可以扩展它以准确返回您需要的内容,但我希望这可以作为一个起点有所帮助。

var faultAreas = new[] { 28, 72, 78 };
var minDate = new DateTime(2011, 04, 01);
var maxDate = DateTime.Now;

var failures =    
    from failure in context.InspectionFailures
    where faultAreas.Contains(failure.FaultAreaID)
    where failure.CreatedDate >= minDate
    where failure.CreatedDate <= maxDate
    select failure;

祝你好运。

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.

var faultAreas = new[] { 28, 72, 78 };
var minDate = new DateTime(2011, 04, 01);
var maxDate = DateTime.Now;

var failures =    
    from failure in context.InspectionFailures
    where faultAreas.Contains(failure.FaultAreaID)
    where failure.CreatedDate >= minDate
    where failure.CreatedDate <= maxDate
    select failure;

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文