Comments (6)
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.
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.
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.
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.
It isn't on NuGet, the previews are on GitHub only.
See Installing a package for details
from nein-linq.
Thank you. I checked, for my cases the problem is fixed
from nein-linq.
Related Issues (20)
- NotSupportedException when using DbInjectable within a sub query HOT 13
- [Nullsafe Queries] Default values for Nullable<T> causing troubles HOT 3
- NeinLinq injects hiding member in injectable member of declaring type
- .CountAsync() and .FirstAsync() throws exception when using ToInjectable HOT 5
- DynamicQuery.CreatePredicate doesn't work with Enums HOT 4
- Query tags are stripped away HOT 3
- Is it possible to store the result of an expression in a property? HOT 4
- Readonly CachedExpressions HOT 1
- Documentation: What does calling WithLambdaInjection() on DbContext options do? HOT 2
- Support For Entity Framework Core 6 HOT 18
- Doc bug HOT 1
- How about an implicit cast to `CachedExpression<X>` from `Expression<X>`? HOT 5
- Property vs method issue HOT 4
- ToInjectable vs ToEntityInjectable HOT 4
- Is `FirstOrDefaultAsync` supported HOT 1
- Q: would this work with source generators? HOT 2
- [Question/Feature Request] Overwrite member/property mapping HOT 1
- [Help] Translating from LinqKit Invoke + Expand to NeinLinq HOT 1
- Issue with Moq HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nein-linq.