Entity Framework v4.1 LIKE

前端 未结 3 2178
清酒与你
清酒与你 2020-12-06 18:00

How do I have to build my query to result in an output SQL query like:

SELECT
[viewRegisters].[Id] AS [IdRegister]
WHERE Name LIKE \'%a%bc\'
<
3条回答
  •  我在风中等你
    2020-12-06 18:34

    If you are using SQL Server, use the PATINDEX function to do a pattern search. You can access this function through EF using the SqlFunctions class.

    For example, the following EF query

    context.ViewRegisters.Where(z => SqlFunctions.PatIndex("a%b%c%", z.Name) > 0);
    

    will translate into

    SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name]
    FROM [dbo].[ViewRegisters] AS [Extent1]
    WHERE (CAST(PATINDEX(N'a%b%c%', [Extent1].[Name]) AS int)) > 0
    

提交回复
热议问题