尝试加入linq-to-sql中的视图时出错

时间:2010-09-21 16:18:46

标签: linq-to-sql

尝试通过将其与其他两个表连接来从视图中提取数据时,我收到以下错误:“SQL Server无法处理NText,Text,Xml或Image数据类型的比较。”

以下是查询:

    var expeditions = from VE in context.ViewExpeditions
                      join SIAGR in context.SiteInAdviseGoodsRef on VE.DeliveryNotes equals SIAGR.Value
                      join SIA in context.SiteInAdvise on SIAGR.SiteInAdviseId equals SIA.Id
                      where SIA.Id == SiteInAdviseID
                      select VE;

以下是View ViewExpeditions:

SELECT ve.*, S.[$Refex] as SiteRefex,c.[$Refex] as ServiceRefex , DeliveryNotes = LEFT(o.list, LEN(o.list)-1),st.Status,st.StatusRefex 
from(
SELECT     
B.[$Id] AS Booking,
B.[$Refex] AS BookingRefex,
B.SiguxCPUE AS CartaDePorte,
SUM(ISNULL(BG.Weight, 0)) AS Weight,
SUM(ISNULL(BG.Volume, 0)) AS Volume,
ISNULL( SUBSTRING(B.Comments, 1, 100),'')  AS Comments, 
'Type' = 
      CASE 
         WHEN PickupSite IS NULL THEN 'Pickup'

         ELSE 'Delivery'
      END,
'Name' = 
      CASE 
         WHEN PickupSite IS NULL THEN PickupName

         ELSE DeliveryName
      END,
      'City' = 
      CASE 
         WHEN PickupSite IS NULL THEN PickupCity

         ELSE DeliveryCity
      END,
'PostalCode' = 
      CASE 
         WHEN PickupSite IS NULL THEN PickupPostalCode

         ELSE DeliveryPostalCode
      END,

'ContactName' = 
      CASE 
         WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactName,'')

         ELSE ISNULL(B.DeliveryContactName,'')
      END,
 'ContactPhone' = 
      CASE 
         WHEN PickupSite IS NULL THEN ISNULL(B.PickupContactPhone,'')

         ELSE ISNULL( B.DeliveryContactPhone,'')
      END,



B.PickupDate AS DataExp,
B.DeliveryDate,
coalesce( B.PickupSite,B.DeliverySite) as 'Site',
b.Service,isnull(B.SiguxState,0) as SiguxState

FROM         dbo.BookingGoods AS BG 
INNER JOIN   dbo.Booking AS B ON BG.BookingId = B.[$Id] 

WHERE     (B.ExecutedBy = 2) AND B.SiguxCPUE is not null AND B.[$IsDeleted]=0
GROUP BY  B.PickupCity,b.PickupContactName,b.PickupContactPhone,b.PickupName,b.PickupPostalCode, B.DeliveryName,B.DeliverySite,  B.DeliveryCity, B.DeliveryPostalCode, B.DeliveryContactName, B.DeliveryContactPhone, B.PickupDate,  B.[$Id], B.SiguxCPUE, B.[$Refex], B.Comments,b.PickupSite,B.Service,B.SiguxState,B.DeliveryDate

) ve 
INNER join [ViewBookingActualStatus] st on st.Booking=ve.Booking
INNER join Service c on ve.Service=c.[$Id]
INNER JOIN dbo.Site AS S ON S.[$Id] = ve.Site   
 outer APPLY 
( 
        SELECT distinct
            CONVERT(VARCHAR(12), dbo.BookingGoodsRef.Value) + ', ' AS [text()] 
        FROM 
            dbo.BookingGoodsRef 
        WHERE 
            dbo.BookingGoodsRef.BookingId = ve.Booking and BookingGoodsRef.Type=13

        FOR XML PATH('') 
    ) o (list) 

我在哪里错了?

注意:如果我尝试在linqpad中运行查询,它根本不会给我任何错误,并生成以下sql:

SELECT [t0].[Booking], [t0].[BookingRefex], [t0].[CartaDePorte], [t0].[Weight], [t0].[Volume], [t0].[Comments], [t0].[Type], [t0].[Name], [t0].[City], [t0].[PostalCode], [t0].[ContactName], [t0].[ContactPhone], [t0].[DataExp], [t0].[DeliveryDate], [t0].[Site], [t0].[Service], [t0].[SiguxState], [t0].[SiteRefex], [t0].[ServiceRefex], [t0].[DeliveryNotes], [t0].[Status], [t0].[StatusRefex]
FROM [ViewExpeditions] AS [t0]
INNER JOIN [SiteInAdviseGoodsRef] AS [t1] ON [t0].[DeliveryNotes] = [t1].[Value]
INNER JOIN [SiteInAdvise] AS [t2] ON [t1].[SiteInAdviseId] = [t2].[$Id]
WHERE [t2].[$Id] = @p0

1 个答案:

答案 0 :(得分:0)

如果您将生成的SQL从LinqPad复制到SSMS并尝试运行它,您会得到您期望的结果吗?

我猜您加入的其中一列(可能是DeliveryNotes)是ntexttext。 SQL Server无法加入text列 - 您必须将两列都投放到varchar或在两列上使用substring