2009-01-07 9 views
1

J'ai une table de DB avec la structure suivante:LINQ 2 SQL, Wrong génération SQL

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

La requête LINQ suivante:

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 
          }; 

Génère l'instruction SQL suivante

{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] 

La requête est erronée après cela. Company devrait être Company, Cluster, BU, etc. mais c'est Company dans tous les cas.

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))) 

}

Quelqu'un at-il fait face à un problème similaire?

+1

J'ai reformaté votre question. Vous avez utilisé beaucoup de HTML, ce qui, j'en suis sûr, prend du temps. Je pense que vous aurez avantage à lire comment publier des questions en utilisant [Markdown] (http://daringfireball.net/projects/markdown/) sur http://stackoverflow.com/editing-help. Cela nous facilitera la lecture et la compréhension de vos questions et, à son tour, vous donnera de meilleures réponses. – smartcaveman

Répondre

3

Je pense que ceci est votre problème:

(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)) 

Ne devrait-il:

(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

Oups ... stupide moi. Va vérifier à nouveau et voir si c'est ce que j'ai écrit. –

+0

Correction du typpo, cela ne fonctionne toujours pas. –

0

Je réécrit votre LINQ avec l'équivalent d'un LEFT OUTER JOIN. Je pense que quelque chose comme ça est ce que vous cherchez. Je ne peux pas vraiment vérifier le SQL généré car je n'ai pas votre DB.

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 
}; 

Je suppose que vous pouvez ajouter un .Single() après la sous-requête dans le Join, mais je ne sais pas comment cela va affecter vos résultats. Faites-moi savoir si cela fonctionne!