2010-04-22 92 views
1

這很奇怪。 我正在將存儲的proc移動到服務中。 TSQL聯合了多個選擇。爲了複製這個,我創建了多個查詢,生成一個通用的新的具體類型。然後我發出一個return result.ToString();將得到的SQL選擇具有變化從而指定引起MSSQL消息205列數...Linq To Sql Concat()在創建的TSQL中刪除字段

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

上述代碼在一個複雜的選擇語句,其中上述每個「表」具有不同的列計數的結果。 (請參閱下面的SQL)我一直在嘗試一些東西,但沒有改變。想法是受歡迎的。

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] 

回答

1

認爲這是LINQ to SQL中的一個錯誤。你可以在這裏找到它:

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

希望給有適合你的解決方法。

+0

我認爲你是對的。我嘗試在所有列上使用ToString(),但沒有奏效。我分別運行查詢,然後在結果上使用concat。據我所知,這有效。 幸運的是,查詢是這樣的,變通方法並沒有太大的改變。我會檢查這方面的表現。 – secoast 2010-04-22 21:34:03

+0

聽起來不錯..希望它能解決問題。祝你好運 :-) – Raja 2010-04-23 00:49:36