2009-01-07 67 views
1

我有以下结构的数据库表:LINQ 2 SQL,错误的SQL生成

Id   int (identity) 
Company  string 
Cluster  string 
BU   string 
Department string 
SalesPoint string 

以下LINQ查询:

var chEntities = from a in dataContext.CompanyHierarchies         
let parent = (dataContext.CompanyHierarchies.Where(ch => ch.Id == companyHierarchyId).Single()) 
where 
( 
           (a.Company == (parent.Company == null ? a.Company : parent.Company)) && 
           (a.Cluster == (parent.Cluster == null ? a.Cluster : parent.Cluster)) && 
           (a.Company == (parent.BU == null ? a.BU : parent.BU)) && 
           (a.Company == (parent.Department == null ? a.Department : parent.Department)) && 
           (a.Company == (parent.SalesPoint == null ? a.SalesPoint : parent.SalesPoint))         
          ) 
          select new CompanyHierarchyEntity 
          { 
           Id = a.Id, 
           Name = a.SalesPoint == null ? (a.BU == null ? (a.Cluster == null ? (a.Company) : a.Cluster) : a.BU) : a.SalesPoint, 
           CompanyHierarchyLevel = (CompanyHierarchyLevel)a.HierarchyLevel, 
           Company = a.Company, 
           Cluster = a.Cluster, 
           BU = a.BU, 
           Department = a.Department, 
           Section = a.SalesPoint 
          }; 

生成以下SQL

{SELECT 
(CASE 
     WHEN [t0].[SalesPoint] IS NULL THEN 
     (CASE 
      WHEN [t0].[BU] IS NULL THEN 
       (CASE 
        WHEN [t0].[Cluster] IS NULL THEN [t0].[Company] 
        ELSE [t0].[Cluster] 
       END) 
      ELSE [t0].[BU] 
     END) 
    ELSE [t0].[SalesPoint] 
END) AS [Name], [t0].[Id], [t0].[HierarchyLevel] AS [CompanyHierarchyLevel], [t0].[Company], [t0].[Cluster], [t0].[BU], [t0].[Department], [t0].[SalesPoint] AS [Section] 

此后查询是错误的。 Company应该是Company,Cluster,BU等,但在所有情况下都是Company

FROM [dbo].[CompanyHierarchy] AS [t0] 
WHERE ([t0].[Company] = (
(CASE 
    WHEN ((
     SELECT [t1].[Company] 
     FROM [dbo].[CompanyHierarchy] AS [t1] 
     WHERE [t1].[Id] = @p0 
     )) IS NULL THEN [t0].[Company] 
    ELSE (
     SELECT [t2].[Company] 
     FROM [dbo].[CompanyHierarchy] AS [t2] 
     WHERE [t2].[Id] = @p0 
     ) 
    END))) AND ([t0].[Cluster] = (
(CASE 
    WHEN ((
     SELECT [t3].[Cluster] 
     FROM [dbo].[CompanyHierarchy] AS [t3] 
     WHERE [t3].[Id] = @p0 
     )) IS NULL THEN [t0].[Cluster] 
    ELSE (
     SELECT [t4].[Cluster] 
     FROM [dbo].[CompanyHierarchy] AS [t4] 
     WHERE [t4].[Id] = @p0 
     ) 
END))) AND ([t0].[Company] = (
(CASE 
    WHEN ((
     SELECT [t5].[BU] 
     FROM [dbo].[CompanyHierarchy] AS [t5] 
     WHERE [t5].[Id] = @p0 
     )) IS NULL THEN [t0].[BU] 
    ELSE (
     SELECT [t6].[BU] 
     FROM [dbo].[CompanyHierarchy] AS [t6] 
     WHERE [t6].[Id] = @p0 
     ) 
END))) AND ([t0].[Company] = (
(CASE 
    WHEN ((
     SELECT [t7].[Department] 
     FROM [dbo].[CompanyHierarchy] AS [t7] 
     WHERE [t7].[Id] = @p0 
     )) IS NULL THEN [t0].[Department] 
    ELSE (
     SELECT [t8].[Department] 
     FROM [dbo].[CompanyHierarchy] AS [t8] 
     WHERE [t8].[Id] = @p0 
     ) 
END))) AND ([t0].[Company] = (
(CASE 
    WHEN ((
     SELECT [t9].[SalesPoint] 
     FROM [dbo].[CompanyHierarchy] AS [t9] 
     WHERE [t9].[Id] = @p0 
     )) IS NULL THEN [t0].[SalesPoint] 
    ELSE (
     SELECT [t10].[SalesPoint] 
     FROM [dbo].[CompanyHierarchy] AS [t10] 
     WHERE [t10].[Id] = @p0 
     ) 
END))) 

}

有没有其他人遇到类似的问题?

+1

我重新格式化了你的问题。您使用了很多HTML,我相信这很耗时。我想你会通过阅读关于如何使用[Markdown](http://daringfireball.net/projects/markdown/)在http://stackoverflow.com/editing-help上发布问题来获益。这将使我们更容易阅读和理解您的问题,并反过来给您更好的答案。 – smartcaveman 2011-06-14 13:16:21

回答

3

我觉得这是你的问题:

(a.Company == (parent.BU == null ? a.BU : parent.BU)) && 
(a.Company == (parent.Department == null ? a.Department : parent.Department)) && 
(a.Company == (parent.SalesPoint == null ? a.SalesPoint : parent.SalesPoint)) 

它不应该是:

(a.BU == (parent.BU == null ? a.BU : parent.BU)) && 
(a.Department == (parent.Department == null ? a.Department : parent.Department)) && 
(a.SalesPoint == (parent.SalesPoint == null ? a.SalesPoint : parent.SalesPoint)) 
+0

糟糕...我愚蠢。 将再次检查,看看我是否写了。 – 2009-01-07 18:43:41

0

我改写了你的LINQ与LEFT OUTER JOIN的等价物。我认为这就是你要找的东西。我无法检查生成的SQL,因为我没有数据库。

var chEntities = from a in dataContext.CompanyHierarchies 
join parent in (from ch in dataContext.CompanHeirarchies 
       where ch.Id == companyHierarchyId 
       select ch) on 
    new {a.Company, a.Cluster, a.BU, a.Department, a.SalesPoint} 
    equals new {parent.Company, parent.Cluster, parent.BU, parent.Department, parent.SalesPoint} 
    into temp 
from t in temp.DefaultIfEmpty() 
select new CompanyHierarchyEntity 
{ 
Id = a.Id, 
Name = a.SalesPoint == null ? (a.BU == null ? (a.Cluster == null ? (a.Company) : a.Cluster) : a.BU) : a.SalesPoint, 
CompanyHierarchyLevel = (CompanyHierarchyLevel)a.HierarchyLevel, 
Company = a.Company, 
Cluster = a.Cluster, 
BU = a.BU, 
Department = a.Department, 
Section = a.SalesPoint 
}; 

我想你可以在Join子查询后添加.Single(),但我不知道怎么会影响您的结果。

让我知道它是否有效!