2011-08-18 59 views
2

我需要这个SQL查询转换为LINQ查询,我也需要公开SQL选择属性:转换这个SQL查询来LINQ查询

SELECT Problem.ProblemID, ProblemFactory.ObjectiveID, Objective.Name, ProblemFactory.Time, ProblemType.ProblemTypeName, ProblemFactory.OperationID, 
        ProblemFactory.Range1ID, ProblemFactory.Range2ID, ProblemFactory.Range3ID, ProblemFactory.Range4ID, 
        ProblemFactory.MissingNumber 
FROM Problem INNER JOIN ProblemFactory ON Problem.ProblemFactoryID = ProblemFactory.ProblemFactoryID 
      INNER JOIN ProblemType ON ProblemFactory.ProblemTypeID = ProblemType.ProblemTypeID 
      INNER JOIN Objective ON Objective.ObjectiveID = ProblemFactory.ObjectiveID 

更新1:

这是什么我有:

 var query = from problem in dc.Problem2s 
        from factory 
        in dc.ProblemFactories 
         .Where(v => v.ProblemFactoryID == problem.ProblemFactoryID) 
         .DefaultIfEmpty() 
        from ... 

而且我用这个例子:What is the syntax for an inner join in LINQ to SQL?

+0

好的,那你的问题是什么?我们不会为你做你的工作... – cdhowie

+0

我知道,虽然这只是一个疑问。我没有和LINQ一起工作得很好。我需要一点帮助。问题是我使用三个内部连接,我不知道如何在LINQ中转换它 – Darf

+0

我更新了文章 – Darf

回答

3

像这样的东西?

var query = 
    from p in ctx.Problem 
    join pf in ctx.ProblemFactory on p.ProblemFactoryID equals pf.ProblemFactoryID 
    join pt in ctx.ProblemType on pf.ProblemTypeID equals pt.ProblemTypeID 
    join o in ctx.Objective on pf.ObjectiveID equals o.ObjectiveID 
    select new 
    { 
     p.ProblemID, 
     pf.ObjectiveID, 
     o.Name, 
     pf.Time, 
     pt.ProblemTypeName, 
     pf.OperationID, 
     pf.Range1ID, 
     pf.Range2ID, 
     pf.Range3ID, 
     pf.Range4ID, 
     pf.MissingNumber, 
    }; 

但你是什么意思的“SQL选择属性”?

+0

我认为我在找什么。我在我的数据库中使用了存储过程,但是我意识到我无法获取属性,因为我使用了多个表。所以你在选择新的方面做了什么,你揭露了这些属性,这就是我需要的。 – Darf

0

像Linq-to-SQL这样的ORM的好处之一就是我们不必将我们的数据拼凑起来从数据库中检索它。如果您在设计师(即,如果你有他们的关系映射)映射你的对象,你应该能够只获得了​​,然后根据需要获取相关的属性...

var problems = from problem in dc.Problem2s select problem; 

foreach (var problem in problems) 
{ 
    // you can work with the problem, its objective, and its problem type. 
    problem.DoThings(); 
    var objective = problem.Objective; 
    var type = problem.ProblemType;  
} 

因此,你保留一个逻辑数据层中的数据结构,而不是无法轻易传递的匿名类型。

+0

明天我会测试一下。现在是午夜 – Darf