Error when trying to join a view in linq-to-sql
While trying to extract data from a view by joining it with two other tables, I'm getting the following error: "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."
And here is the query:
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;
Here is the 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 Picku开发者_开发技巧pSite 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)
Where am I going wrong about this?
NOTE: If I try to run the query in linqpad, it doesn't give me any errors at all, and generates the following 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
If you copy the generated SQL from LinqPad into SSMS and try to run it, do you get the results you expect?
I'd guess that the one of the columns on which you're joining (probably DeliveryNotes
) is ntext
or text
. SQL Server can't join on text
columns - you have to either cast both columns to varchar
or use a substring
on both columns.
精彩评论