Entity framework 使用查询表达式进行Linq排序
是否可以使用基于动态字符串参数的linq查询表达式执行Entity framework 使用查询表达式进行Linq排序,entity-framework,linq,linq-to-sql,Entity Framework,Linq,Linq To Sql,是否可以使用基于动态字符串参数的linq查询表达式执行orderby表达式?因为我的查询产生了奇怪的SQL查询 我的林克: var product = from prod in _context.Products join cat in _context.Categories on prod.CategoryId equals cat.CategoryId join sup in _context.Suppliers on prod.Sup
orderby
表达式?因为我的查询产生了奇怪的SQL查询
我的林克:
var product = from prod in _context.Products
join cat in _context.Categories on prod.CategoryId equals cat.CategoryId
join sup in _context.Suppliers on prod.SupplierId equals sup.SupplierId
orderby sortParam
select new ProductViewModel
{
ProductName = prod.ProductName,
ProductId = prod.ProductId,
QuantityPerUnit = prod.QuantityPerUnit,
ReorderLevel = prod.ReorderLevel,
UnitsOnOrder = prod.UnitsOnOrder,
UnitPrice = prod.UnitPrice,
UnitsInStock = prod.UnitsInStock,
Discontinued = prod.Discontinued,
Category = cat.CategoryName,
Supplier = sup.CompanyName,
CategoryId = cat.CategoryId,
SupplierId = sup.SupplierId
};
其中var sortParam=“prod.ProductName”
上面的代码生成奇怪的sql,其中order by sortParam被转换为(选择1)
。sql探查器捕获的完整查询如下:
exec sp_executesql N'SELECT [prod].[ProductName], [prod].[ProductID], [prod].[QuantityPerUnit], [prod].[ReorderLevel], [prod].[UnitsOnOrder], [prod].[UnitPrice], [prod].[UnitsInStock], [prod].[Discontinued], [cat].[CategoryName] AS [Category], [sup].[CompanyName] AS [Supplier], [cat].[CategoryID], [sup].[SupplierID]
FROM [Products] AS [prod]
INNER JOIN [Categories] AS [cat] ON [prod].[CategoryID] = [cat].[CategoryID]
INNER JOIN [Suppliers] AS [sup] ON [prod].[SupplierID] = [sup].[SupplierID]
ORDER BY (SELECT 1)
OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY',N'@__p_1 int,@__p_2 int',@__p_1=0,@__p_2=10
我看到很多人使用动态参数进行linq排序,但他们都使用lambda而不是查询表达式,请告诉我如前所述,您传递的是字符串值,而不是反映列名的表达式。但是,您可以选择所需的内容,请参见示例。
sortParam
是一个字符串变量,在查询过程中具有常量值,按常量值排序与不排序相同。