Giter Site home page Giter Site logo

Create parameterized queries about gridify HOT 4 CLOSED

VahidN avatar VahidN commented on May 18, 2024 2
Create parameterized queries

from gridify.

Comments (4)

alirezanet avatar alirezanet commented on May 18, 2024 1

This feature is available in version v2.2.1.
To activate Query optimization for entity framework you need to Enable the EntityFrameworkCompatibilityLayer

  GridifyGlobalConfiguration.EnableEntityFrameworkCompatibilityLayer();

e.g

 _dbContext.Users.ApplyFiltering("name = vahid")

when EntityFrameworkCompatibilityLayer is enable the output is

DECLARE @__Value_0 nvarchar(4000) = N'vahid';

SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = @__Value_0

from gridify.

alirezanet avatar alirezanet commented on May 18, 2024

Hi, Does this query translate to a parameterized Sql query using SqlServer provider?

dbContext.Blogs.Where( Param_0 =>  Param_0.Title == "action")

The thing is gridify doesn't control the provider's behavior. if you have a filter like title=action gridify generates the above query so you should see the same results in native LINQ and gridify generated expression,
I will look into this more, but first I need to find out what is causing the provider different behavior between native LINQ and gridify.

from gridify.

alirezanet avatar alirezanet commented on May 18, 2024

I did some research and few tests. the behavior of the Native LINQ and gridify generated expression is the same in SqlServer Provider.

// using SqlServer provider
     [Fact]
      public void ApplyFiltering_GeneratedSqlShouldMatch_SqlServerProvider()
      {
         var actual = _dbContext.Users.ApplyFiltering("name = vahid").ToQueryString();
         var expected = _dbContext.Users.Where(q => q.Name == "vahid").ToQueryString();

         Assert.Equal(expected , actual);
      }
SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = N'vahid'

according to this test, the question is, does even this feature you mentioned exist in Entity Framework ?! if yes, how we can use it because looks like we can not use it even with Native LINQ., did I missed something?

from gridify.

alirezanet avatar alirezanet commented on May 18, 2024

Never mind, I found the parameterized way to do this in LINQ.
This test is failing because of additional parameter exist in the final query:

      [Fact]
      public void ApplyFiltering_GeneratedSqlShouldMatch_UsingVariable_SqlServerProvider()
      {
         var name = "vahid";
         var actual = _dbContext.Users.ApplyFiltering("name = vahid").ToQueryString();
         var expected = _dbContext.Users.Where(q => q.Name == name).ToQueryString();

         Assert.Equal(expected , actual);
      }
DECLARE @__name_0 nvarchar(4000) = N'vahid';

SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = @__name_0

I never encountered any best practices in the entity framework that says you should always define at least a variable for your constant values!

from gridify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.