是否可以混合使用 LINQ 和动态字符串查询来编写 EF Core 查询?
Is it possible to mix writing an EF Core query with LINQ and a dynamic string query?
我希望能够在 LINQ 中编写大部分查询,但随后将 WHERE
子句写为字符串,就像动态查询一样。所以像:
var query = from part in _context.CurrentInventory
query.Where = "part.PartNumber LIKE '%a%' OR part.PartNumber LIKE '%b%'
原因是因为 WHERE 会变得非常大,使用 EF.Functions.Like
和 Contains
确实会减慢查询速度。
我可以动态构建整个东西并只执行字符串,但我希望避免这种情况。
你可以使用System.Linq.Dynamic which is based on以前的作品
Install-Package System.Linq.Dynamic
查询
query.Where = "part.PartNumber LIKE '%a%' OR part.PartNumber LIKE '%b%'
可以re-written为:
query.Where ("part.PartNumber.Contains(@0) or part.PartNumber.Contains(@1)","a" ,"b")
的工作示例
更新:
我将动态 Linq 与 EntityFramework
结合使用
using (var context = new NorthwindEntities())
{
var customers = context.Customers.Where("ContactName.Contains(@0) or ContactName.Contains(@1)", "Maria","Carine").ToList();
Console.WriteLine(customers.Count);
}
生成的 Sql(从 sql 分析器中看到)是:
SELECT
[Extent1].[CustomerID] AS [CustomerID],
[Extent1].[CompanyName] AS [CompanyName],
[Extent1].[ContactName] AS [ContactName],
[Extent1].[ContactTitle] AS [ContactTitle],
[Extent1].[Address] AS [Address],
[Extent1].[City] AS [City],
[Extent1].[Region] AS [Region],
[Extent1].[PostalCode] AS [PostalCode],
[Extent1].[Country] AS [Country],
[Extent1].[Phone] AS [Phone],
[Extent1].[Fax] AS [Fax]
FROM [dbo].[Customers] AS [Extent1]
WHERE ([Extent1].[ContactName] LIKE N'%Maria%') OR ([Extent1].[ContactName] LIKE N'%Carine%')
where条件翻译为:
WHERE ([Extent1].[ContactName] LIKE N'%Maria%') OR ([Extent1].[ContactName] LIKE N'%Carine%')
我希望能够在 LINQ 中编写大部分查询,但随后将 WHERE
子句写为字符串,就像动态查询一样。所以像:
var query = from part in _context.CurrentInventory
query.Where = "part.PartNumber LIKE '%a%' OR part.PartNumber LIKE '%b%'
原因是因为 WHERE 会变得非常大,使用 EF.Functions.Like
和 Contains
确实会减慢查询速度。
我可以动态构建整个东西并只执行字符串,但我希望避免这种情况。
你可以使用System.Linq.Dynamic which is based on以前的作品
Install-Package System.Linq.Dynamic
查询
query.Where = "part.PartNumber LIKE '%a%' OR part.PartNumber LIKE '%b%'
可以re-written为:
query.Where ("part.PartNumber.Contains(@0) or part.PartNumber.Contains(@1)","a" ,"b")
的工作示例
更新:
我将动态 Linq 与 EntityFramework
结合使用 using (var context = new NorthwindEntities())
{
var customers = context.Customers.Where("ContactName.Contains(@0) or ContactName.Contains(@1)", "Maria","Carine").ToList();
Console.WriteLine(customers.Count);
}
生成的 Sql(从 sql 分析器中看到)是:
SELECT
[Extent1].[CustomerID] AS [CustomerID],
[Extent1].[CompanyName] AS [CompanyName],
[Extent1].[ContactName] AS [ContactName],
[Extent1].[ContactTitle] AS [ContactTitle],
[Extent1].[Address] AS [Address],
[Extent1].[City] AS [City],
[Extent1].[Region] AS [Region],
[Extent1].[PostalCode] AS [PostalCode],
[Extent1].[Country] AS [Country],
[Extent1].[Phone] AS [Phone],
[Extent1].[Fax] AS [Fax]
FROM [dbo].[Customers] AS [Extent1]
WHERE ([Extent1].[ContactName] LIKE N'%Maria%') OR ([Extent1].[ContactName] LIKE N'%Carine%')
where条件翻译为:
WHERE ([Extent1].[ContactName] LIKE N'%Maria%') OR ([Extent1].[ContactName] LIKE N'%Carine%')