从.NET Core 2.1更新到3.1后,我收到错误:无效操作异常

deikduxw  于 2023-02-26  发布在  .NET
关注(0)|答案(1)|浏览(142)

我得到这个错误有人能帮助我吗?
无效操作异常: LINQ表达式"DbSet.Join(外部:数据集,内部:o =〉EF.属性(o,"列表ID"),外部键选择器:l =〉EF.属性(l,"Id"),内部键选择器:(o,i)=〉new TransparentIdentifier〈Order,Listing〉(Outer = o,Inner = i))。其中(o =〉o. Outer. Accepted &&!(o. Outer. Cancelled)&& o. Outer. EndDateTime〈DateTime. Now && o. Inner. Active)'无法转换。请以可转换的格式重写查询,或通过插入对AsEnumerable()、AsyncEnumerable()、ToList()或ToListAsync()。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。 (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.
我尝试运行的查询如下所示:

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));

我试着补充道:AsEnumerable()AsAsyncEnumerable()ToList()ToListAsync(),但它们都无法正常工作。

    • 回答我**

我得到这个错误,因为清单包含

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

将查询更改为正常工作后:

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));
vfh0ocws

vfh0ocws1#

如果实体列表在其中一个属性中包含=〉表达式,请尝试检查您的代码。lambda表达式不是在服务器端计算,而是在用户端计算,您可以在LINQ表达式中使用它,这就是为什么您会得到:无效操作异常。

相关问题