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:
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.