我有一个存储过程,我正在尝试将其转换为 Linq 到 LLBLGen 查询。Linq to LLBGen 中的查询有效,但是当我跟踪发送到 sql server 的查询时,它远非完美。
这是存储过程:
ALTER PROCEDURE [dbo].[spDIGI_GetAllUmbracoProducts]
-- Add the parameters for the stored procedure.
@searchText nvarchar(255),
@startRowIndex int,
@maximumRows int,
@sortExpression nvarchar(255) AS BEGIN
SET @startRowIndex = @startRowIndex + 1
SET @searchText = '%' + @searchText + '%'
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- This is the query which will fetch all the UmbracoProducts.
-- This query also supports paging and sorting.
WITH UmbracoOverview As
(
SELECT ROW_NUMBER() OVER(
ORDER BY
CASE
WHEN @sortExpression = 'productName' THEN umbracoProduct.productName
WHEN @sortExpression = 'productCode' THEN umbracoProduct.productCode
END ASC,
CASE
WHEN @sortExpression = 'productName DESC' THEN umbracoProduct.productName
WHEN @sortExpression = 'productCode DESC' THEN umbracoProduct.productCode
END DESC )
AS row_num, umbracoProduct.umbracoProductId, umbracoProduct.productName, umbracoProduct.productCode
FROM umbracoProduct INNER JOIN
product ON umbracoProduct.umbracoProductId = product.umbracoProductId
WHERE (umbracoProduct.productName LIKE @searchText
OR umbracoProduct.productCode LIKE @searchText
OR product.code LIKE @searchText
OR product.description LIKE @searchText
OR product.descriptionLong LIKE @searchText
OR product.unitCode LIKE @searchText)
)
SELECT UmbracoOverview.UmbracoProductId, UmbracoOverview.productName, UmbracoOverview.productCode
FROM UmbracoOverview
WHERE (row_num >= @startRowIndex
AND row_num < (@startRowIndex + @maximumRows))
-- This query will count all the UmbracoProducts.
-- This query is used for paging inside ASP.NET.
SELECT COUNT (umbracoProduct.umbracoProductId) AS CountNumber
FROM umbracoProduct INNER JOIN
product ON umbracoProduct.umbracoProductId = product.umbracoProductId
WHERE (umbracoProduct.productName LIKE @searchText
OR umbracoProduct.productCode LIKE @searchText
OR product.code LIKE @searchText
OR product.description LIKE @searchText
OR product.descriptionLong LIKE @searchText
OR product.unitCode LIKE @searchText) END
这是我的 Linq 到 LLBLGen 查询:
using System.Linq.Dynamic;
var q = (
from up in MetaData.UmbracoProduct
join p in MetaData.Product on up.UmbracoProductId equals p.UmbracoProductId
where up.ProductCode.Contains(searchText) ||
up.ProductName.Contains(searchText) ||
p.Code.Contains(searchText) ||
p.Description.Contains(searchText) ||
p.DescriptionLong.Contains(searchText) ||
p.UnitCode.Contains(searchText)
select new UmbracoProductOverview
{
UmbracoProductId = up.UmbracoProductId,
ProductName = up.ProductName,
ProductCode = up.ProductCode
}
).OrderBy(sortExpression);
//Save the count in HttpContext.Current.Items. This value will only be saved during 1 single HTTP request.
HttpContext.Current.Items["AllProductsCount"] = q.Count();
//Returns the results paged.
return q.Skip(startRowIndex).Take(maximumRows).ToList<UmbracoProductOverview>();
这是我要处理的初始表达式:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource`1[Eurofysica.DB.EntityClasses.UmbracoProductEntity]).Join(value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource`1[Eurofysica.DB.EntityClasses.ProductEntity]), up => up.UmbracoProductId, p => p.UmbracoProductId, (up, p) => new <>f__AnonymousType0`2(up = up, p = p)).Where(<>h__TransparentIdentifier0 => (((((<>h__TransparentIdentifier0.up.ProductCode.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText) || <>h__TransparentIdentifier0.up.ProductName.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText)) || <>h__TransparentIdentifier0.p.Code.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText)) || <>h__TransparentIdentifier0.p.Description.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText)) || <>h__TransparentIdentifier0.p.DescriptionLong.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText)) || <>h__TransparentIdentifier0.p.UnitCode.Contains(value(Eurofysica.BusinessLogic.BLL.Controllers.UmbracoProductController+<>c__DisplayClass1).searchText))).Select(<>h__TransparentIdentifier0 => new UmbracoProductOverview() {UmbracoProductId = <>h__TransparentIdentifier0.up.UmbracoProductId, ProductName = <>h__TransparentIdentifier0.up.ProductName, ProductCode = <>h__TransparentIdentifier0.up.ProductCode}).OrderBy( => .ProductName).Count()
现在这是查询发送到 sql server 的样子:
选择查询:
Query: SELECT [LPA_L2].[umbracoProductId] AS [UmbracoProductId], [LPA_L2].[productName] AS [ProductName], [LPA_L2].[productCode] AS [ProductCode] FROM ( [eurofysica].[dbo].[umbracoProduct] [LPA_L2] INNER JOIN [eurofysica].[dbo].[product] [LPA_L3] ON [LPA_L2].[umbracoProductId] = [LPA_L3].[umbracoProductId]) WHERE ( ( ( ( ( ( ( ( [LPA_L2].[productCode] LIKE @ProductCode1) OR ( [LPA_L2].[productName] LIKE @ProductName2)) OR ( [LPA_L3].[code] LIKE @Code3)) OR ( [LPA_L3].[description] LIKE @Description4)) OR ( [LPA_L3].[descriptionLong] LIKE @DescriptionLong5)) OR ( [LPA_L3].[unitCode] LIKE @UnitCode6))))
参数:@ProductCode1:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@ProductName2:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@Code3:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@Description4:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@DescriptionLong5:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@UnitCode6:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。
计数查询:
Query: SELECT TOP 1 COUNT(*) AS [LPAV_] FROM (SELECT [LPA_L2].[umbracoProductId] AS [UmbracoProductId], [LPA_L2].[productName] AS [ProductName], [LPA_L2].[productCode] AS [ProductCode] FROM ( [eurofysica].[dbo].[umbracoProduct] [LPA_L2] INNER JOIN [eurofysica].[dbo].[product] [LPA_L3] ON [LPA_L2].[umbracoProductId] = [LPA_L3].[umbracoProductId]) WHERE ( ( ( ( ( ( ( ( [LPA_L2].[productCode] LIKE @ProductCode1) OR ( [LPA_L2].[productName] LIKE @ProductName2)) OR ( [LPA_L3].[code] LIKE @Code3)) OR ( [LPA_L3].[description] LIKE @Description4)) OR ( [LPA_L3].[descriptionLong] LIKE @DescriptionLong5)) OR ( [LPA_L3].[unitCode] LIKE @UnitCode6))))) [LPA_L1]
参数:@ProductCode1:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@ProductName2:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@Code3:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@Description4:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@DescriptionLong5:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。参数:@UnitCode6:字符串。长度:2。精度:0。比例:0。方向:输入。价值: ”%%”。
如您所见,没有进行排序或分页(就像在我的存储过程中一样)。这可能是在获取所有结果后在代码内部完成的。这会消耗很多性能!有人知道我如何以正确的方式将我的存储过程转换为 Linq 到 LLBLGen 吗?