Quality of generated code

Jun 21, 2008 at 12:43 AM
Hi guys,

Just a quick question - when passing any EF query via InterLinq it changes from something like this;

SELECT
'0X0X' AS [C1],
[Extent1].[BusinessEntityID] AS [BusinessEntityID],
[Extent2].[Name] AS [Name],
[Extent2].[CreatedDate] AS [CreatedDate],
[Extent2].[ModifiedDate] AS [ModifiedDate],
[Extent2].[Flags] AS [Flags],
[Extent1].[AccountID] AS [AccountID],
[Extent1].[Status] AS [Status]
FROM  [dbo].[Order] AS [Extent1]
INNER JOIN [dbo].[BaseEntity] AS [Extent2] ON [Extent1].[BusinessEntityID] = [Extent2].[BusinessEntityID]

into the following monstrosity....

SELECT
CASE WHEN ([UnionAll3].[C12] = 1) THEN '0X0X' WHEN ([UnionAll3].[C11] = 1) THEN '0X1X' WHEN ([UnionAll3].[C13] = 1) THEN '0X2X' ELSE '0X3X' END AS [C1],
[UnionAll3].[C1] AS [C2],
[Extent2].[Name] AS [Name],
[Extent2].[CreatedDate] AS [CreatedDate],
[Extent2].[ModifiedDate] AS [ModifiedDate],
[Extent2].[Flags] AS [Flags],
CASE WHEN ([UnionAll3].[C12] = 1) THEN [UnionAll3].[C2] WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS decimal(19,4)) WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS decimal(19,4)) END AS [C3],
CASE WHEN ([UnionAll3].[C12] = 1) THEN [UnionAll3].[C3] WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS bit) WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS bit) END AS [C4],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS uniqueidentifier) WHEN ([UnionAll3].[C11] = 1) THEN [UnionAll3].[C4] WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS uniqueidentifier) END AS [C5],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS uniqueidentifier) WHEN ([UnionAll3].[C11] = 1) THEN [UnionAll3].[C5] WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS uniqueidentifier) END AS [C6],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS int) WHEN ([UnionAll3].[C11] = 1) THEN [UnionAll3].[C6] WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS int) END AS [C7],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS uniqueidentifier) WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS uniqueidentifier) WHEN ([UnionAll3].[C13] = 1) THEN [UnionAll3].[C7] END AS [C8],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS int) WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS int) WHEN ([UnionAll3].[C13] = 1) THEN [UnionAll3].[C8] END AS [C9],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS bit) WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS bit) WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS bit) ELSE [UnionAll3].[C9] END AS [C10],
CASE WHEN ([UnionAll3].[C12] = 1) THEN CAST(NULL AS decimal(19,4)) WHEN ([UnionAll3].[C11] = 1) THEN CAST(NULL AS decimal(19,4)) WHEN ([UnionAll3].[C13] = 1) THEN CAST(NULL AS decimal(19,4)) ELSE [UnionAll3].[C10] END AS [C11]
FROM   (SELECT
 [UnionAll2].[C1] AS [C1],
 [UnionAll2].[C2] AS [C2],
 [UnionAll2].[C3] AS [C3],
 [UnionAll2].[C4] AS [C4],
 [UnionAll2].[C5] AS [C5],
 [UnionAll2].[C6] AS [C6],
 [UnionAll2].[C7] AS [C7],
 [UnionAll2].[C8] AS [C8],
 [UnionAll2].[C9] AS [C9],
 [UnionAll2].[C10] AS [C10],
 [UnionAll2].[C11] AS [C11],
 [UnionAll2].[C12] AS [C12],
 [UnionAll2].[C13] AS [C13]
 FROM  (SELECT
  [UnionAll1].[C1] AS [C1],
  [UnionAll1].[C2] AS [C2],
  [UnionAll1].[C3] AS [C3],
  [UnionAll1].[C4] AS [C4],
  [UnionAll1].[C5] AS [C5],
  [UnionAll1].[C6] AS [C6],
  [UnionAll1].[C7] AS [C7],
  [UnionAll1].[C8] AS [C8],
  [UnionAll1].[C9] AS [C9],
  [UnionAll1].[C10] AS [C10],
  [UnionAll1].[C11] AS [C11],
  [UnionAll1].[C12] AS [C12],
  [UnionAll1].[C13] AS [C13]
  FROM  (SELECT
   [Project1].[C1] AS [C1],
   CAST(NULL AS decimal(18,0)) AS [C2],
   CAST(NULL AS bit) AS [C3],
   [Project1].[C2] AS [C4],
   [Project1].[C3] AS [C5],
   [Project1].[C4] AS [C6],
   CAST(NULL AS uniqueidentifier) AS [C7],
   CAST(NULL AS int) AS [C8],
   CAST(NULL AS bit) AS [C9],
   CAST(NULL AS decimal(18,0)) AS [C10],
   cast(1 as bit) AS [C11],
   cast(0 as bit) AS [C12],
   cast(0 as bit) AS [C13]
   FROM ( SELECT
    CAST(NULL AS uniqueidentifier) AS [C1],
    CAST(NULL AS uniqueidentifier) AS [C2],
    CAST(NULL AS uniqueidentifier) AS [C3],
    CAST(NULL AS int) AS [C4]
    FROM  ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
   )  AS [Project1]
   WHERE 1 = 0
  UNION ALL
   SELECT
   [Extent1].[BusinessEntityID] AS [BusinessEntityID],
   CAST(NULL AS decimal(18,0)) AS [C1],
   CAST(NULL AS bit) AS [C2],
   CAST(NULL AS uniqueidentifier) AS [C3],
   CAST(NULL AS uniqueidentifier) AS [C4],
   CAST(NULL AS int) AS [C5],
   [Extent1].[AccountID] AS [AccountID],
   [Extent1].[Status] AS [Status],
   CAST(NULL AS bit) AS [C6],
   CAST(NULL AS decimal(18,0)) AS [C7],
   cast(0 as bit) AS [C8],
   cast(0 as bit) AS [C9],
   cast(1 as bit) AS [C10]
   FROM [dbo].[Order] AS [Extent1]) AS [UnionAll1]
 UNION ALL
  SELECT
  [Project5].[C1] AS [C1],
  [Project5].[C2] AS [C2],
  [Project5].[C3] AS [C3],
  CAST(NULL AS uniqueidentifier) AS [C4],
  CAST(NULL AS uniqueidentifier) AS [C5],
  CAST(NULL AS int) AS [C6],
  CAST(NULL AS uniqueidentifier) AS [C7],
  CAST(NULL AS int) AS [C8],
  CAST(NULL AS bit) AS [C9],
  CAST(NULL AS decimal(18,0)) AS [C10],
  cast(0 as bit) AS [C11],
  cast(1 as bit) AS [C12],
  cast(0 as bit) AS [C13]
  FROM ( SELECT
   CAST(NULL AS uniqueidentifier) AS [C1],
   CAST(NULL AS decimal(19,4)) AS [C2],
   CAST(NULL AS bit) AS [C3]
   FROM  ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable2]
  )  AS [Project5]
  WHERE 1 = 0) AS [UnionAll2]
UNION ALL
 SELECT
 [Project8].[C1] AS [C1],
 CAST(NULL AS decimal(18,0)) AS [C2],
 CAST(NULL AS bit) AS [C3],
 CAST(NULL AS uniqueidentifier) AS [C4],
 CAST(NULL AS uniqueidentifier) AS [C5],
 CAST(NULL AS int) AS [C6],
 CAST(NULL AS uniqueidentifier) AS [C7],
 CAST(NULL AS int) AS [C8],
 [Project8].[C2] AS [C9],
 [Project8].[C3] AS [C10],
 cast(0 as bit) AS [C11],
 cast(0 as bit) AS [C12],
 cast(0 as bit) AS [C13]
 FROM ( SELECT
  CAST(NULL AS uniqueidentifier) AS [C1],
  CAST(NULL AS bit) AS [C2],
  CAST(NULL AS decimal(19,4)) AS [C3]
  FROM  ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable3]
 )  AS [Project8]
 WHERE 1 = 0) AS [UnionAll3]
INNER JOIN [dbo].[BaseEntity] AS [Extent2] ON [UnionAll3].[C1] = [Extent2].[BusinessEntityID]


Two things;

1. Any ideas why?
2. Can I prevent this?

many thanks

Rob