ef core 3.0 query with join table from DB and list from memory fails

一笑奈何 提交于 2021-01-29 10:00:35

问题


I wrote simple query, which should find images with hashes already presented in database. So "images" is List from memory and _context.AdPics is table from DB

 var picsThatAlreadyInDb = (from pic in _context.AdPics
                join image in images on pic.ImgHash equals image.ImgHash
                select new {DbPicId=pic.ID, newImage = image}).ToList();

I get this error:

System.InvalidOperationException : Processing of the LINQ expression 'DbSet<AdPic>
    .Join(
        outer: __p_0, 
        inner: pic => pic.ImgHash, 
        outerKeySelector: image => image.ImgHash, 
        innerKeySelector: (pic, image) => new { 
            DbPicId = pic.ID, 
            newImage = image
         })' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at InfraStructure.SaveToDb.Save(ScrapedAd ad, List`1 images) in C:\Users\fjod1\RiderProjects\crawler\InfraStructure\SaveToDb.cs:line 27
   at Tests.SavePicsTest.Test() in C:\Users\fjod1\RiderProjects\crawler\Testing\Tests\SavePicsTest.cs:line 64

So is it a bug/feature in ef core 3.0 or I am doing something completely wrong with such query?


回答1:


Well I rewrote it into 2 parts, first one loads data from db and second one is same linq query which works pretty fine, because it is working with two Lists from memory.

So it looks like it's not possible to do linq join when one list is in memory and another in db.




回答2:


var picsThatAlreadyInDb = (from pic in _context.AdPics.AsEnumerable()
                join image in images on pic.ImgHash equals image.ImgHash
                select new {DbPicId=pic.ID, newImage = image}).ToList();


来源:https://stackoverflow.com/questions/58979239/ef-core-3-0-query-with-join-table-from-db-and-list-from-memory-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!