Giter VIP home page Giter VIP logo

Comments (6)

axelheer avatar axelheer commented on May 19, 2024

That's weird, since the resulting query expressions look almost the same...

List query:

.Call System.Linq.Queryable.Select(
    .Call System.Linq.Queryable.Select(
        .Call .Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[NeinLinq.Tests.TodoList]>(System.Data.Entity.Core.Objects.ObjectQuery`1[NeinLinq.Tests.TodoList]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
        ,
        '(.Lambda #Lambda1<System.Func`2[NeinLinq.Tests.TodoList,<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem]]>))
    ,
    '(.Lambda #Lambda2<System.Func`2[<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem],NeinLinq.Tests.TodoItem]>))

.Lambda #Lambda1<System.Func`2[NeinLinq.Tests.TodoList,<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem]]>(NeinLinq.Tests.TodoList $list)
{
    .New <>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem](
        $list,
        .Call System.Linq.Queryable.FirstOrDefault(.Call System.Linq.Queryable.Where(
                .Call (.Constant<NeinLinq.Tests.SomeTest>(NeinLinq.Tests.SomeTest)._dbContext).Set(),
                '(.Lambda #Lambda3<System.Func`2[NeinLinq.Tests.TodoItem,System.Boolean]>))))
}

.Lambda #Lambda2<System.Func`2[<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem],NeinLinq.Tests.TodoItem]>(<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem] $<>h__TransparentIdentifier0)
{
    $<>h__TransparentIdentifier0.item
}

.Lambda #Lambda3<System.Func`2[NeinLinq.Tests.TodoItem,System.Boolean]>(NeinLinq.Tests.TodoItem $todoItem) {
    $todoItem.ListId == $list.Id
}

Count query:

.Call System.Linq.Queryable.Count(.Call System.Linq.Queryable.Select(
        .Call System.Linq.Queryable.Select(
            .Call .Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[NeinLinq.Tests.TodoList]>(System.Data.Entity.Core.Objects.ObjectQuery`1[NeinLinq.Tests.TodoList]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
            ,
            '(.Lambda #Lambda1<System.Func`2[NeinLinq.Tests.TodoList,<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem]]>))
        ,
        '(.Lambda #Lambda2<System.Func`2[<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem],NeinLinq.Tests.TodoItem]>))
)

.Lambda #Lambda1<System.Func`2[NeinLinq.Tests.TodoList,<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem]]>(NeinLinq.Tests.TodoList $list)
{
    .New <>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem](
        $list,
        .Call System.Linq.Queryable.FirstOrDefault(.Call System.Linq.Queryable.Where(
                .Call (.Constant<NeinLinq.Tests.SomeTest>(NeinLinq.Tests.SomeTest)._dbContext).Set(),
                '(.Lambda #Lambda3<System.Func`2[NeinLinq.Tests.TodoItem,System.Boolean]>))))
}

.Lambda #Lambda2<System.Func`2[<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem],NeinLinq.Tests.TodoItem]>(<>f__AnonymousType3`2[NeinLinq.Tests.TodoList,NeinLinq.Tests.TodoItem] $<>h__TransparentIdentifier0)
{
    $<>h__TransparentIdentifier0.item
}

.Lambda #Lambda3<System.Func`2[NeinLinq.Tests.TodoItem,System.Boolean]>(NeinLinq.Tests.TodoItem $todoItem) {
    $todoItem.ListId == $list.Id
}

The processing of these expressions is completely different between Entity Framework and Entity Framework Core. Happily the newer Framework understands this anyway.

Nevertheless, NeinLinq does some "clean-up" of the query's expression, which does handle properties but not method calls at the moment. Thus, this should work for you already:

public class GetTodoItemsQueryProvider : IQueryProvider<ApplicationDbContext, int, TodoItem>
{
    [InjectLambda(nameof(GetQueryExpression))]
    public IQueryable<TodoItem> GetQuery(ApplicationDbContext context, int query)
    {
        throw new NotImplementedException();
    }

    public Expression<Func<ApplicationDbContext, int, IQueryable<TodoItem>>> GetQueryExpression()
    {
        return (context, query) => from todoItem in context.TodoItems
                                   where todoItem.ListId == query
                                   select todoItem;
    }
}

I can dig a little deeper to make context.Set<TodoItem>() work as well. Please let me know, if context.TodoItems is sufficient anyway.

Just for further reference, the test code I used in order to reproduce this:

using System.Data.Entity;
using Xunit;

namespace NeinLinq.Tests;

public class SomeTest
{
    private readonly ApplicationDbContext _dbContext;
    private readonly IQueryProvider<ApplicationDbContext, int, TodoItem> _queryProvider;

    public SomeTest()
    {
        _dbContext = new ApplicationDbContext("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SomeTest;Integrated Security=true;");
        _dbContext.TodoLists.Add(
            new TodoList
            {
                Items =
                {
                    new TodoItem()
                }
            }
        );
        _dbContext.SaveChanges();

        _queryProvider = new GetTodoItemsQueryProvider();
    }

    [Fact]
    public void Test_WithLambdaInjection()
    {
        var query = from list in _dbContext.Set<TodoList>().ToDbInjectable()
                    let item = _queryProvider.GetQuery(_dbContext, list.Id).FirstOrDefault()
                    select item;

        var result = query.ToList();

        Assert.NotEmpty(result);

        int count = query.Count();

        Assert.NotEqual(0, count);
    }

    [Fact]
    public void Test_WithoutLambdaInjection()
    {
        var query = from list in _dbContext.Set<TodoList>()
                    let item = (from todoItem in _dbContext.Set<TodoItem>()
                                where todoItem.ListId == list.Id
                                select todoItem).FirstOrDefault()
                    select item;

        var result = query.ToList();

        Assert.NotEmpty(result);

        int count = query.Count();

        Assert.NotEqual(0, count);
    }
}

public class TodoList
{
    public int Id { get; set; }

    public virtual ICollection<TodoItem> Items { get; } = new List<TodoItem>();
}

public class TodoItem
{
    public int Id { get; set; }

    public int ListId { get; set; }

    public virtual TodoList? List { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public DbSet<TodoList> TodoLists { get; }

    public DbSet<TodoItem> TodoItems { get; }

    public ApplicationDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {
        TodoLists = Set<TodoList>();
        TodoItems = Set<TodoItem>();
    }
}

public interface IQueryProvider<DbContext, TParam, TResult>
{
    [InjectLambda]
    IQueryable<TResult> GetQuery(DbContext context, TParam query);
}

public class GetTodoItemsQueryProvider : IQueryProvider<ApplicationDbContext, int, TodoItem>
{
    [InjectLambda(nameof(GetQueryExpression))]
    public IQueryable<TodoItem> GetQuery(ApplicationDbContext context, int query)
    {
        throw new NotImplementedException();
    }

    public Expression<Func<ApplicationDbContext, int, IQueryable<TodoItem>>> GetQueryExpression()
    {
        return (context, query) => from todoItem in context.TodoItems
                                   where todoItem.ListId == query
                                   select todoItem;
    }
}

from nein-linq.

TheHACKATHON avatar TheHACKATHON commented on May 19, 2024

Thanks for your research.
сontext.TodoItems works as a workaround for now, but I would like to use Set<>, because in most code we inject abstract DbContext and don't have access to properties like TodoItems, and unit tests now based on Set method

from nein-linq.

axelheer avatar axelheer commented on May 19, 2024

Okay, after some further digging, it seems, that Entity Framework needs some processing, in order to understand, what you are doing here. This is currently implemented by an internal EF class DbQueryVisitor, which I now call using some Reflection.

I'm not quite happy with this "Hack", but please try Version 6.2.0 as it will be available as a Preview here:
https://github.com/axelheer/nein-linq/pkgs/nuget/NeinLinq.EntityFramework

For implementation details: 66817a2

from nein-linq.

TheHACKATHON avatar TheHACKATHON commented on May 19, 2024

For some reason, I can't see this prerelease on NuGet and can't install it via command
NU1102: Unable to find package NeinLinq.EntityFramework with version (>= 6.2.0-preview.186)

from nein-linq.

axelheer avatar axelheer commented on May 19, 2024

It isn't on NuGet, the previews are on GitHub only.

See Installing a package for details

from nein-linq.

TheHACKATHON avatar TheHACKATHON commented on May 19, 2024

Thank you. I checked, for my cases the problem is fixed

from nein-linq.

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.