我是 Linq 的新手,并试图将此 SQL Server 存储过程转换为 Linq,我正在构建一个 Silverlight 业务应用程序,需要调用此过程以返回结果网格。
我有多个参数,用户可以使用这些参数来搜索特定的片段。他们通过 UI 缩小搜索范围,当他们点击搜索按钮时,后面的代码会获取所有参数并将其发送到我的 Linq 服务,然后该服务需要调用存储过程。
这是存储过程。
ALTER PROCEDURE dbo.spSearchResults
@PieceType nvarchar(6) = '',
@FileType nvarchar(3) = '',
@Market nvarchar(6) = '',
@PieceNumber nvarchar(6) = '',
@Header1 nvarchar(50) = '',
@Header2 nvarchar(50) = '',
@Header3 nvarchar(50) = '',
@Header4 nvarchar(50) = '',
@JobNumber nvarchar(50)=' ',
@bShowInActive BIT = 0,
@UDAC1 nvarchar(50) = '',
@UDAC2 nvarchar(50) = '',
@UDAC3 nvarchar(50) = '',
@UDAC4 nvarchar(50) = ''
AS
BEGIN
SET NOCOUNT ON
SELECT J.*
FROM Job J
LEFT JOIN JobHeading H1 (NOLOCK) ON J.[JobNumber] = H1.[JobID]
LEFT JOIN JobHeading H2 (NOLOCK) ON J.[JobNumber] = H2.[JobID]
LEFT JOIN JobHeading H3 (NOLOCK) ON J.[JobNumber] = H3.[JobID]
LEFT JOIN JobHeading H4 (NOLOCK) ON J.[JobNumber] = H4.[JobID]
LEFT JOIN JobUDAC udac1 (NOLOCK) ON J.[JobNumber] = udac1.[JobID]
LEFT JOIN JobUDAC udac2 (NOLOCK) ON J.[JobNumber] = udac2.[JobID]
LEFT JOIN JobUDAC udac3 (NOLOCK) ON J.[JobNumber] = udac3.[JobID]
LEFT JOIN JobUDAC udac4 (NOLOCK) ON J.[JobNumber] = udac4.[JobID]
WHERE ((@PieceType = '') OR (PieceType = @PieceType))
AND ((@FileType = '') OR (FileType = @FileType))
AND ((@Market = '') OR (Market = @Market))
AND ((@PieceNumber = '') OR (PieceNumber = @PieceNumber))
AND ((@JobNumber = '') OR (JobNumber = @JobNumber))
AND (J.IsActive=1 OR @bShowInActive = 1)
AND (((@Header1 = '' AND @Header2 = '' AND @Header3 = '' AND @Header4 = '') OR
H1.HeadingRowID = @Header1)
OR (--@Header2=0 OR
H2.HeadingRowID = @Header2 )
OR (--@Header3=0 OR
H3.HeadingRowID = @Header3)
OR (--@Header4=0 OR
H4.HeadingRowID = @Header4))
AND (((@UDAC1 = '' AND @UDAC2 = '' AND @UDAC3 = '' AND @UDAC4 = '') OR
udac1.UDACRowID = @UDAC1)
OR (--@Header2=0 OR
udac2.UDACRowID = @UDAC2 )
OR (--@Header3=0 OR
udac3.UDACRowID = @UDAC3)
OR (--@Header4=0 OR
udac4.UDACRowID = @UDAC4))
在 Linq 中,我发现有一些转换要做,这是我的尝试。
var query = from j in Job
join JobHeading H1 in Job on headingRowID1 equals H1
join JobHeading H2 in Job on headingRowID2 equals H2
join JobHeading H3 in Job on headingRowID3 equals H3
join JobHeading H4 in Job on headingRowID4 equals H4
join JobUDAC udac1 in Job on udacRowID1 equals udac1
join JobUDAC udac2 in Job on udacRowID2 equals udac2
join JobUDAC udac3 in Job on udacRowID3 equals udac3
join JobUDAC udac4 in Job on udacRowID4 equals udac4
join PieceType in db on piece equals PieceType
join JobFileType in db on filetype equals JobFileType
join Book in db on market equals Book
join PieceNumber in db on pieceNumber equals PieceNumber
join JobNumber in db on jobNumber equals JobNumber
join Job in db on FindJobs equals db
where ((piece = string.Empty) || (PieceType = piece))
&& ((filetype = string.Empty) || (JobFileType = filetype))
&& ((market = string.Empty) || (Book = market))
&& ((pieceNumber = string.Empty) || (PieceNumber = pieceNumber))
&& ((jobNumber = string.Empty) || (JobNumber = jobNumber))
&& (showInActive = true)
&& ((((headingRowID1 = string.Empty) + (headingRowID2 = string.Empty) + (headingRowID3 = string.Empty) + (headingRowID4 = string.Empty)) ||
H1.HeadingRowID = headingRowID1)
|| (H2.HeadingRowID = headingRowID2)
|| (H3.HeadingRowID = headingRowID3)
|| (H4.HeadingRowID = headingRowID4))
&& ((((udacRowID1 = string.Empty) + (udacRowID2 = string.Empty) + (udacRowID3 = string.Empty) + (udacRowID4 = string.Empty)) ||
udac1.UDACRowID = udacRowID1)
|| (udac2.UDACRowID = udacRowID2)
|| (udac3.UDACRowID = udacRowID3)
|| (udac4.UDACRowID = udacRowID4))
select j.Job;
return query;
但是,开头的'Job'有一个错误,并说'找不到实现......'join' not found' 谁能帮忙翻译一下?或者提供一种更好的方法来调用带有后面代码的存储过程?谢谢