Home > Mobile >  EF version updated to 3.1.22 from 2.1 breaks Linq
EF version updated to 3.1.22 from 2.1 breaks Linq

Time:01-29

I am upgrading my ASP.NET Core Web API project from .NET Core 2.1 to 3.1. I also needed to upgrade EF Core from 2.1 to 3.1.

After upgrading EF, the Linq queries which were working in .NET Core 2.1 are not working (might be backward compatibility not supported with new version or change in using Linq).

What are standard ways to rewrite linq queries to make it functional? Which things in linq changed from 2.1 to 3.1? The Issue is related to select clause having operation related to ANY() etc.

    var classes =
        (
            from z in Context.aaa.Where(x => x.Id == Id)
            join b in Context.bbb on z.Id equals b.Id
            join bc in Context.ccc.Where(x => x.IsActive == true) on b.Id equals bc.Id
            join fcc in Context.ddd.Where(x => x.IsActive == true) on bc.Id equals fcc.Id
            join fc in Context.eee.Where(x => x.IsActive == true )
                .Include(i => i.Level) on fcc.Id equals fc.Id
            select new
            {
                bc.IsUserRequired,
                IsHiddenByOverrideOrExclusion = zoneList.Any(x => x.Id == fc.Id && x.IsHidden == true) || (!zoneList.Any(x => x.Id == fc.Id && x.IsHidden == false) && functioncheckFlag(fc, Id, category)),
            }
        ).Distinct().AsEnumerable();

CodePudding user response:

EF Core 3.x have changed translation behavior. EF Core no longer silently process data on the client side. So translation will fail because functioncheckFlag cannot be converted to the SQL and, probably, zoneList.Any also if zoneList is local collection.

To solve problem, you have to do Distinct on the client side.

var classes =
    (
        from z in Context.aaa.Where(x => x.Id == Id)
        join b in Context.bbb on z.Id equals b.Id
        join bc in Context.ccc.Where(x => x.IsActive == true) on b.Id equals bc.Id
        join fcc in Context.ddd.Where(x => x.IsActive == true) on bc.Id equals fcc.Id
        join fc in Context.eee.Where(x => x.IsActive == true ) on fcc.Id equals fc.Id
        select new
        {
            bc.IsUserRequired,
            IsHiddenByOverrideOrExclusion = zoneList.Any(x => x.Id == fc.Id && x.IsHidden == true) || (!zoneList.Any(x => x.Id == fc.Id && x.IsHidden == false) && functioncheckFlag(fc, Id, category)),
        }
    )
    .AsEnumerable()
    .Distinct();

Also removed Include, it is not needed if you have Select at the end of the query.

  •  Tags:  
  • Related