我有一个存储过程,我正在尝试将其转换为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 to 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:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ ProductName2:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ Code3:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ Description4:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ DescriptionLong5:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ UnitCode6:String。长度: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:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ ProductName2:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ Code3:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ Description4:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ DescriptionLong5:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。 参数:@ UnitCode6:String。长度:2。精度:0。比例:0。方向:输入。值:“%%”。
正如您所看到的那样,没有进行排序或分页(就像在我的存储过程中一样)。这可能是在获取所有结果后在代码内部完成的。这会花费很多性能!有谁知道如何将我的存储过程转换为Linq到LLBLGen的正确方法?
答案 0 :(得分:0)
您是否尝试使用TakePage()而不是Skip / Take?文档说这是首选,我遇到了Skip / Take的一些奇怪问题。
我认为OrderBy子句在return语句中可能更好?我怀疑这是问题的根源。
所以也许试试:
return q.OrderBy(sortExpression).TakePage(pageNumber, pageSize);