C# 无法在foreach中翻译LINQ表达式
我无法将特定值输入到C# 无法在foreach中翻译LINQ表达式,c#,asp.net,.net,entity-framework,C#,Asp.net,.net,Entity Framework,我无法将特定值输入到Untis\u Prof字段中 代码示例: foreach (EnseignantReport test in grades) { test.Untis_Prof = this._dbContext.Teachers.FirstOrDefaultAsync(c => test.Untis_Prof == string.Join(" ", new[] {c.LastName, c.FirstName })) .
Untis\u Prof
字段中
代码示例:
foreach (EnseignantReport test in grades)
{
test.Untis_Prof = this._dbContext.Teachers.FirstOrDefaultAsync(c =>
test.Untis_Prof == string.Join(" ", new[] {c.LastName, c.FirstName }))
.Result.UntisCode;
}
错误:
System.InvalidOperationException:LINQ表达式'DbSet()
.Where(t=>\uu progress\u EnteringTeacherName\u 0==string.Format(
格式:“{0}{1}”,
arg0:t.LastName,
arg1:t.FirstName))“
无法翻译。其他信息:翻译方法
'string.Format'
失败。如果此方法可以映射到自定义函数,请参阅以获取更多信息。以可以翻译的形式重写查询,或者通过插入对“AsEnumerable”、“asAsAsAsyncEnumerable”、“ToList”或“ToListSync”的调用显式切换到客户端计算。有关更多信息,请参阅。在Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslationExpressionVisitor.g_uCheckTranslated | 15_0(ShapedQueryExpression已翻译,c_uDisplayClass15_0&)中
在Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslationExpressionVisitor.VisitMethodCall调用(MethodCallExpression MethodCallExpression)
有人能帮我吗?这对你不管用吗
foreach (EnseignantReport test in grades)
{
test.Untis_Prof = this._dbContext.Teachers.FirstOrDefaultAsync(c =>
test.Untis_Prof == c.LastName + " " + c.FirstName }))
.Result.UntisCode;
}
Linq到DB,不理解如何转换字符串。请连接到数据库查询。您必须解决此问题,或者获取内存中的所有教师this.\u dbContext.teachers.AsEnumerable
,然后使用c#代码查询可枚举内容
顺便说一句,使用
.Result
不是一个好的做法,如果不能使用wait
,最好使用非异步FirstOrDefault
函数。您正处于死锁的危险中。您显示的错误消息与代码示例不匹配(代码示例中没有字符串。Format
),您是否尝试只创建字符串而不调用其他方法来创建该字符串?也许如果你做了这样的事情:$“{c.LastName}{c.FirstName}”
(或者c.LastName+“”+c.FirstName
)这能回答你的问题吗?