Is it possible to mix writing an EF Core query with LINQ and a dynamic string query?

c# ef-core-2.0 entity-framework-core

Accepted Answer

Use System.Linq.Dynamic, which is due to's earlier work.

    Install-Package System.Linq.Dynamic

The issue

   query.Where = "part.PartNumber LIKE '%a%' OR part.PartNumber LIKE '%b%'

can also be expressed as:

    query.Where ("part.PartNumber.Contains(@0) or part.PartNumber.Contains(@1)","a" ,"b")

Working illustration at zzz-31 zzz

Update:

I utilized EntityFramework and dynamic Linq.

      using (var context = new NorthwindEntities())
        {
            var customers = context.Customers.Where("ContactName.Contains(@0) or ContactName.Contains(@1)", "Maria","Carine").ToList();
            Console.WriteLine(customers.Count);
        }

The produced SQL is as follows, as seen by sql profiler:

    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%')

The where clause is equivalent to:

     WHERE ([Extent1].[ContactName] LIKE N'%Maria%') OR ([Extent1].[ContactName] LIKE N'%Carine%')  
2
2/27/2018 9:08:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow