我发现最好的解决方案是继续在SQL中创建一个表值函数来生成结果,例如::
CREATE function [dbo].[getMatches](@textStr nvarchar(50)) returns @MatchTbl table(
Fullname nvarchar(50) null,
ID nvarchar(50) null
)
as begin
declare @SearchStr nvarchar(50);
set @SearchStr = '%' + @textStr + '%';
insert into @MatchTbl
select (LName + ', ' + FName + ' ' + MName) AS FullName, ID = ID from employees where LName like @SearchStr;
return;
end
GO
select * from dbo.getMatches('j')
然后,您只需将函数拖到LINQ.dbml设计器中,然后像处理其他对象一样调用它即可。LINQ甚至知道您存储的函数的列。我这样叫出来::
Dim db As New NobleLINQ
Dim LNameSearch As String = txt_searchLName.Text
Dim hlink As HyperLink
For Each ee In db.getMatches(LNameSearch)
hlink = New HyperLink With {.Text = ee.Fullname & "<br />", .NavigateUrl = "?ID=" & ee.ID}
pnl_results.Controls.Add(hlink)
Next
非常简单,真正利用了SQL和LINQ在应用程序中的强大功能……您当然可以生成想要达到相同效果的任何表值函数!