Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

After updating from .NET 2.1 to 3.1 I am getting error: InvalidOperationException

I am getting this error can anyone help me, with this?

InvalidOperationException: The LINQ expression ‘DbSet .Join( outer: DbSet, inner: o => EF.Property<Nullable>(o, "ListingId"), outerKeySelector: l => EF.Property<Nullable>(l, "Id"), innerKeySelector: (o, i) => new TransparentIdentifier<Order, Listing>( Outer = o, Inner = i )) .Where(o => o.Outer.Accepted && !(o.Outer.Cancelled) && o.Outer.EndDateTime < DateTime.Now && o.Inner.Active)’ could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

The querry, that I am trying to run is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

return Task.FromResult((IResultList<ListingDTO>)new OrderByDistance(MapperFilter, ContextFactory.GetSharedContext().Set<Listing>().Where(listing =>
            (string.IsNullOrEmpty(keyword) || listing.Name.Contains(keyword) || listing.Description.Contains(keyword)) &&
            (!categoryIds.Any() || categoryIds.Contains(listing.Category.Id)) &&
            (string.IsNullOrEmpty(zipCode) || listing.User.ZipCode == zipCode) &&
            (!zipCodesInRegion.Any() || zipCodesInRegion.Contains(listing.User.ZipCode)) &&
            (priceFrom == null || listing.Prices.Any(e => e.Amount >= priceFrom && (priceTo == null || e.Amount <= priceTo))) &&
            (priceTo == null || listing.Prices.Any(e => e.Amount <= priceTo && (priceFrom == null || e.Amount >= priceFrom))) &&
            !listing.Deleted &&
            listing.Active &&
            !listing.ImportedNotActivated &&
            listing.Category.Active &&
            listing.User.Active &&
            ((forSale && listing.IsForSale) || !forSale)
            ), latitude, longitude));

I tried adding: AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(), but none of them are working.

Answer
I was getting this error, because the listing do contain

public bool IsForSale => SalesPrice != null || SalesPrice > 0;
 

After changing the Query to it is working:

return Task.FromResult((IResultList<ListingDTO>)new OrderByDistance(MapperFilter, ContextFactory.GetSharedContext().Set<Listing>().Where(listing =>
            (string.IsNullOrEmpty(keyword) || listing.Name.Contains(keyword) || listing.Description.Contains(keyword)) &&
            (!categoryIds.Any() || categoryIds.Contains(listing.Category.Id)) &&
            (string.IsNullOrEmpty(zipCode) || listing.User.ZipCode == zipCode) &&
            (!zipCodesInRegion.Any() || zipCodesInRegion.Contains(listing.User.ZipCode)) &&
            (priceFrom == null || listing.Prices.Any(e => e.Amount >= priceFrom && (priceTo == null || e.Amount <= priceTo))) &&
            (priceTo == null || listing.Prices.Any(e => e.Amount <= priceTo && (priceFrom == null || e.Amount >= priceFrom))) &&
            !listing.Deleted &&
            listing.Active &&
            !listing.ImportedNotActivated &&
            listing.Category.Active &&
            listing.User.Active &&
            ((forSale && (SalesPrice != null || SalesPrice > 0)) || !forSale)
            ), latitude, longitude));

>Solution :

Try checking your code, if the entity Listing do contain => expressions in one of the properties.
The lambda expression is evaluated not on a server side, but on a user, and you can use it in LINQ expression and that’s why You are getting the: InvalidOperationException.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading