gpt4 book ai didi

C# Linq 表达式无法翻译

转载 作者:行者123 更新时间:2023-12-03 08:48:18 27 4
gpt4 key购买 nike

我正在尝试执行 linq 查询来获取所有具有某些特定技能的员工。 search.skills 是具有某些技能的字符串列表,我想检索具有所有这些技能(和条件)的所有用户。在我关于员工的 where 子句中,exp.Skills 是 ICollection,expSkill.SkillName 是技能名称

                .Where(
emp => search.Skills.All(
searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill)
))
.ToListAsync();

我在尝试执行它时收到以下错误。我正在使用entityframework core 3

The LINQ expression 'DbSet<Employee>
.Where(e => __search_Skills_0
.All(searchSkill => DbSet<Experience>
.Where(e0 => EF.Property<Nullable<Guid>>(e, "Id") != null && EF.Property<Nullable<Guid>>(e, "Id") == EF.Property<Nullable<Guid>>(e0, "EmployeeId"))
.SelectMany(
source: e0 => DbSet<ExperienceSkill>
.Where(e1 => EF.Property<Nullable<Guid>>(e0, "EmployeeId") != null && new AnonymousObject(new object[]
{
(object)EF.Property<Nullable<Guid>>(e0, "EmployeeId"),
(object)EF.Property<string>(e0, "ProjectCode")
}) == new AnonymousObject(new object[]
{
(object)EF.Property<Nullable<Guid>>(e1, "ExperienceEmployeeId"),
(object)EF.Property<string>(e1, "ExperienceProjectCode")
})),
collectionSelector: (e0, c) => new TransparentIdentifier<Experience, ExperienceSkill>(
Outer = e0,
Inner = c
))
.Select(ti => ti.Inner.SkillName)
.Contains(searchSkill)))' 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.

谁能告诉我我在查询中做错了什么?谢谢

最佳答案

EF Core 目前无法在 search.Skills 等内存集合上转换除简单 Contains 之外的 LINQ 运算符。

因此您需要找到 memoryCollection.All 运算符的替代方案。在这种情况下,我使用的是“计数匹配”方法,该方法查找所有匹配记录并比较不同值的计数。

例如替换

emp => search.Skills.All(
searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill)

emp => emp.Experiences
.SelectMany(exp => exp.Skills, (exp, skill) => skill.SkillName)
.Where(skillName => search.Skills.Contains(skillName))
.Distinct().Count() == search.Skills.Distinct().Count()

关于C# Linq 表达式无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com