为什么Entity Framework无法转换此LINQ查询?“无法转换LINQ表达式,”

insrf1ej  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(153)

为什么这个查询不能被翻译?只有一个"First()"可以翻译,两个不行:
例外情况:
无法转换LINQ表达式"DbSet"(此处为整个查询)。请以可以转换的形式重写查询,或者通过插入对"AsEnumerable"、"AsAsAsyncEnumerable"、"ToList"或"ToListAsync *"的调用来显式切换到客户端计算

await RecipesComparatorContext.ItemRecipesComparisonResults
    .Where(compResult => (compResult.IdItemRecipeFromNavigation // From or two the same, both exist
                                                   .ItemRecipesTreeIdItemRecipeParentNavigations.First() 
                                                   .ItemRecipesTreesRoots.First()
                                                   .IdItemRecipeRootNavigation
                                                   .MRPublishedVersions.First().PlantName == plantName)
                                                   
                            )
            )
    .ExecuteUpdateAsync(result => result.SetProperty(r => r.InvalidState, true));

如果它是用ToListAsync()解决的,我应该把它放在哪里?
模型如下:

public partial class ItemRecipesComparisonResult
{
    public int IdItemRecipesComparisonResult { get; set; }
    public int IdItemRecipeFrom { get; set; }
    public int IdItemRecipeTo { get; set; }
    public bool InvalidState { get; set; }
    public virtual ItemRecipe IdItemRecipeFromNavigation { get; set; }
    public virtual ItemRecipe IdItemRecipeToNavigation { get; set; }
}

public partial class ItemRecipe
{
    public ItemRecipe()
    {
        ItemRecipesTreeIdItemRecipeChildNavigations = new HashSet<ItemRecipesTree>();
        ItemRecipesTreeIdItemRecipeParentNavigations = new HashSet<ItemRecipesTree>();
        MRPublishedVersions = new HashSet<MRPublishedVersion>();
    }

    public int IdItemRecipe { get; set; }
    
    public virtual ICollection<ItemRecipesTree> ItemRecipesTreeIdItemRecipeChildNavigations { get; set; }
    public virtual ICollection<ItemRecipesTree> ItemRecipesTreeIdItemRecipeParentNavigations { get; set; }
    
    public virtual ICollection<ItemRecipesTreesRoot> ItemRecipesTreesRoots { get; set; }
    public virtual ICollection<MRPublishedVersion> MRPublishedVersions { get; set; }
}

public partial class ItemRecipesTree
{
    public ItemRecipesTree()
    {
        ItemRecipeInstances = new HashSet<ItemRecipeInstance>();
        ItemRecipesTreesRoots = new HashSet<ItemRecipesTreesRoot>();
    }

    public int IdItemRecipeParent { get; set; }
    public int IdItemRecipeChild { get; set; }
    public virtual ItemRecipe IdItemRecipeChildNavigation { get; set; }
    public virtual ItemRecipe IdItemRecipeParentNavigation { get; set; }
    public virtual ICollection<ItemRecipesTreesRoot> ItemRecipesTreesRoots { get; set; }
}

public partial class ItemRecipesTreesRoot
{
    public int IdItemRecipeTreeRoot { get; set; }
    public int IdItemRecipeParent { get; set; }
    public int IdItemRecipeChild { get; set; }
    public int IdItemRecipeRoot { get; set; }
    public virtual ItemRecipesTree IdItemRecipeNavigation { get; set; }
    public virtual ItemRecipe IdItemRecipeRootNavigation { get; set; }
}

public partial class MRPublishedVersion
{
    public int idVersion {get;set;}
    public string PlantName {get;set;}
}
rsaldnfx

rsaldnfx1#

按照以下方式重写查询,它应该是可翻译的:

var itemsToUpdate = RecipesComparatorContext.ItemRecipesComparisonResults
    .Where(c => c.IdItemRecipeFromNavigation.ItemRecipesTreeIdItemRecipeParentNavigations 
        .SelectMany(p => p.ItemRecipesTreesRoots)
        .SelectMany(r => r.IdItemRecipeRootNavigation.MRPublishedVersions)
        .Any(v => v.PlantName == plantName)
    );

await itemsToUpdate.ExecuteUpdateAsync(result => result.SetProperty(r => r.InvalidState, true));

相关问题