2010-04-22 6 views
1

Ceci est étrange. Je déplace un proc stocké vers un service. Les syndicats TSQL sélectionnent plusieurs fois. Pour répliquer cela, j'ai créé plusieurs requêtes résultant en un nouveau type concret commun. Ensuite, j'émets un résultat return.ToString(); et les sélectionne SQL résultantes ont un nombre variable de colonnes spécifiées provoquant ainsi une MSSQL Msg 205 ...Linq To Sql Concat() suppression de champs dans TSQL créé

using (var db = GetDb()) 
     { 
      var fundInv = from f in db.funds 
          select 
            new Investments 
             { 
               Company = f.company, 
               FullName = f.fullname, 
               Admin = f.admin, 
               Fund = f.fund1, 
               FundCode = f.fundcode, 
               Source = STR_FUNDS, 
               IsPortfolio = false, 
               IsActive = f.active, 
               Strategy = f.strategy, 
               SubStrategy = f.substrategy, 
               AltStrategy = f.altstrategy, 
               AltSubStrategy = f.altsubstrategy, 
               Region = f.region, 
               AltRegion = f.altregion, 
               UseAlternate = f.usealt, 
               ClassesAllowed = f.classallowed 
             }; 



      var stocksInv = from s in db.stocks 
          where !fundInv.Select(f => f.Company).Contains(s.vehcode)         select 
            new Investments 
             { 
               Company = s.company, 
               FullName = s.issuer, 
               Admin = STR_PRS, 
               Fund = s.shortname, 
               FundCode = s.vehcode, 
               Source = STR_STOCK, 
               IsPortfolio = false, 
               IsActive = (s.inactive == null), 
               Strategy = s.style, 
               SubStrategy = s.substyle, 
               AltStrategy = s.altstyle, 
               AltSubStrategy = s.altsubsty, 
               Region = s.geography, 
               AltRegion = s.altgeo, 
               UseAlternate = s.usealt, 
               ClassesAllowed = STR_GENERIC 
             }; 


      var bondsInv = from oi in db.bonds 
          where !fundInv.Select(f => f.Company).Contains(oi.vehcode) 
          select 
            new Investments 
             { 
               Company = string.Empty, 
               FullName = oi.issue, 
               Admin = STR_PRS1, 
               Fund = oi.issue, 
               FundCode = oi.vehcode, 
               Source = STR_BONDS, 
               IsPortfolio = false, 
               IsActive = oi.closed, 
               Strategy = STR_OTH, 
               SubStrategy = STR_OTH, 
               AltStrategy = STR_OTH, 
               AltSubStrategy = STR_OTH, 
               Region = STR_OTH, 
               AltRegion = STR_OTH, 
               UseAlternate = false, 
               ClassesAllowed = STR_GENERIC 
             }; 

      return (fundInv.Concat(stocksInv).Concat(bondsInv)).ToList(); 
     } 

Le code ci-dessus donne une instruction select complexe où chaque « table » a DIFFÉRENT nombre de colonnes. (voir SQL ci-dessous) J'ai essayé quelques choses mais pas encore de changement. Les idées sont les bienvenues.

SELECT [t6].[company] AS [Company], 
    [t6].[fullname] AS [FullName], 
    [t6].[admin] AS [Admin], 
    [t6].[fund] AS [Fund], 
    [t6].[fundcode] AS [FundCode], 
    [t6].[value] AS [Source], 
    [t6].[value2] AS [IsPortfolio], 
    [t6].[active] AS [IsActive], 
    [t6].[strategy] AS [Strategy], 
    [t6].[substrategy] AS [SubStrategy], 
    [t6].[altstrategy] AS [AltStrategy], 
    [t6].[altsubstrategy] AS [AltSubStrategy], 
    [t6].[region] AS [Region], 
    [t6].[altregion] AS [AltRegion], 
    [t6].[usealt] AS [UseAlternate], 
    [t6].[classallowed] AS [ClassesAllowed] 
FROM (
    SELECT [t3].[company], 
     [t3].[fullname], 
     [t3].[admin], 
     [t3].[fund], 
     [t3].[fundcode], 
     [t3].[value], 
     [t3].[value2], 
     [t3].[active], 
     [t3].[strategy], 
     [t3].[substrategy], 
     [t3].[altstrategy], 
     [t3].[altsubstrategy], 
     [t3].[region], 
     [t3].[altregion], 
     [t3].[usealt], 
     [t3].[classallowed] 
FROM (
    SELECT [t0].[company], 
     [t0].[fullname], 
     [t0].[admin], 
     [t0].[fund], 
     [t0].[fundcode], 
     @p0 AS [value], 
     [t0].[active], 
     [t0].[strategy], 
     [t0].[substrategy], 
     [t0].[altstrategy], 
     [t0].[altsubstrategy], 
     [t0].[region], 
     [t0].[altregion], 
     [t0].[usealt], 
     [t0].[classallowed] 
    FROM [zInvest].[funds] AS [t0] 
    UNION ALL 
    SELECT [t1].[company], 
     [t1].[issuer], 
     @p6 AS [value], 
     [t1].[shortname], 
     [t1].[vehcode], 
     @p7 AS [value2], 
     @p8 AS [value3], 
     (CASE 
      WHEN [t1].[inactive] IS NULL THEN 1 
      ELSE 0 
     END) AS [value5], 
     [t1].[style], 
     [t1].[substyle], 
     [t1].[altstyle], 
     [t1].[altsubsty], 
     [t1].[geography], 
     [t1].[altgeo], 
     [t1].[usealt], 
     @p10 AS [value6] 
    FROM [zBank].[stocks] AS [t1] 
    WHERE (NOT (EXISTS(
     SELECT NULL AS [EMPTY] 
     FROM [zInvest].[funds] AS [t2] 
     WHERE [t2].[company] = [t1].[vehcode] 
     ))) AND ([t1].[vehcode] <> @p2) AND (SUBSTRING([t1].[vehcode], @p3 + 1, @p4) <> @p5) 
    ) AS [t3] 
UNION ALL 
SELECT @p11 AS [value], 
    [t4].[issue], 
    @p12 AS [value2], 
    [t4].[vehcode], 
    @p13 AS [value3], 
    @p14 AS [value4], 
    [t4].[closed], 
    @p16 AS [value6], 
    @p17 AS [value7] 
FROM [zMut].[bonds] AS [t4] 
WHERE NOT (EXISTS(
    SELECT NULL AS [EMPTY] 
    FROM [zInvest].[funds] AS [t5] 
    WHERE [t5].[company] = [t4].[vehcode] 
    )) 
) AS [t6] 

Répondre

1

Pensez que c'est un bogue dans LINQ to SQL. Vous pouvez le trouver ici:

http://connect.microsoft.com/VisualStudio/feedback/details/355734/linq-to-sql-produces-incorrect-tsql-when-using-union-or-concat

espère que la solution donnée il fonctionne pour vous.

+0

Je pense que vous avez raison. J'ai essayé d'utiliser ToString() sur toutes les colonnes mais je n'ai pas travaillé. J'ai couru mes requêtes séparément puis utilisé concat sur les résultats. Cela a fonctionné autant que je peux dire. Heureusement, les requêtes sont telles que la solution de contournement n'a pas pris beaucoup de changement. Je vais vérifier la performance à ce sujet. – secoast

+0

Ça sonne bien ... J'espère que ça marche. bonne chance :-) – Raja