4

我在 MSSQL 2005 中创建了一个函数,我想使用 linq 运行这个函数。我的函数有 2 个参数。定义如下:

USE [MobileGateway]
GO
/****** Object:  UserDefinedFunction [dbo].[Fn_GetNoLocationAddress]    Script Date: 11/07/2012 08:27:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Fn_GetNoLocationAddress] 
(   
    -- Add the parameters for the function here
    @Site nvarchar(255),
    @ReceivedDate int
)
RETURNS @noLocationAddress TABLE (

RequestID int NOT NULL,
Barcode varchar(50) NOT NULL,
AdrID int NOT NULL,
Name varchar(50) NOT NULL,
Street varchar(50) NOT NULL,
HouseNo varchar(4) NOT NULL,
Postal varchar(8) NOT NULL,
City varchar(50) NOT NULL,
Country varchar(50) NOT NULL,
Latitude varchar(50) NOT NULL,
Longitude varchar(50) NOT NULL,
ReveivedDate datetime NOT NULL
)
AS 
BEGIN
    -- Add the SELECT statement with parameter references here
INSERT INTO @noLocationAddress 
SELECT     TOP (100) PERCENT Request1.RequestID, TrackIT.dbo.Sending.Barcode, TrackIT.dbo.Address_View.AdrID, TrackIT.dbo.Address_View.Name, 
                      TrackIT.dbo.Address_View.Street, TrackIT.dbo.Address_View.HouseNo, TrackIT.dbo.Address_View.Postal, TrackIT.dbo.Address_View.City, 
                      TrackIT.dbo.Address_View.Country, Request1.Latitude, Request1.Longitude, Request1.ReceivedDate
FROM         (SELECT DISTINCT RequestID, LTRIM([Content]) AS Barcode, Latitude, Longitude, ReceivedDate
                       FROM          dbo.RequestWithLocation
                       WHERE      (Site LIKE @Site) AND ([Content] <> '') AND (AddressID = '0') AND (ReceivedDate > DATEADD(day, -@ReceivedDate, GETDATE()))) AS Request1 INNER JOIN
                      TrackIT.dbo.Sending ON Request1.Barcode = TrackIT.dbo.Sending.Barcode INNER JOIN
                      TrackIT.dbo.Address_View ON TrackIT.dbo.Sending.DeliveryAdrID = TrackIT.dbo.Address_View.AdrID

ORDER BY TrackIT.dbo.Address_View.AdrID

RETURN

END

如您所见,我有 2 个参数,并且正在返回一个包含信息的表格。但是我需要使用 linq 来执行这个函数。任何人都可以帮忙吗?谢谢

4

1 回答 1

6

我假设您使用的是 dbml (linq to sql) 文件。

将您的用户定义函数从 Visual Studio 中的服务器资源管理器拖放到您的 dbml 中。

然后从您的 dbml 的 datacontext 对象中,您可以直接调用该函数。

例如,如果您的 dbml 文件名为 xyz.dbml,那么您的数据上下文对象的名称将为“XyzDataContext”,除非您已更改它。

然后试试这个。

XyzDataContext db = new XyzDataContext();
List<Fn_GetNoLocationAddressResult> = db.Fn_GetNoLocationAddress("site", 25).ToList();
于 2012-11-07T07:53:54.650 回答