Entity framework core EF核心3.1未正确翻译查询
我有一个问题,我写的,说它是不正确的翻译。 查询如下:Entity framework core EF核心3.1未正确翻译查询,entity-framework-core,Entity Framework Core,我有一个问题,我写的,说它是不正确的翻译。 查询如下: var query = from a in context.PlaceSyncs join b in ( from aa in context.PlaceIdentifiers join bb in context.PlaceSources on aa.Pla
var query = from a in context.PlaceSyncs
join b in (
from aa in context.PlaceIdentifiers
join bb in context.PlaceSources on aa.PlaceSourceId equals bb.PlaceSourceId
select new { Id = aa.PlaceIdentifierId, PlaceKey = aa.PlaceIdentifierKey, PlaceSource = bb.PlaceSourceName }
) on new { a.PlaceKey, PlaceSource = a.PlaceSource.ToLower() } equals new { b.PlaceKey, PlaceSource = b.PlaceSource.ToLower() } into temp
from c in temp.DefaultIfEmpty()
where c == null
select new PlaceSync()
{
PlaceActive = a.PlaceActive,
PlaceAddress = a.PlaceAddress,
PlaceInformation = a.PlaceInformation,
PlaceKey = a.PlaceKey,
PlaceName = ((a.PlaceName.Contains("#") == false) ? a.PlaceName : a.PlaceName.Substring(0, a.PlaceName.IndexOf("#"))).TrimEnd().TrimStart(),
PlaceSource = a.PlaceSource,
PlaceSyncId = a.PlaceSyncId
};
var test = query.Count();
当我在linqpad中运行此程序时,所有内容都会检查出来。
当我在我的应用程序中运行此命令时,会出现以下错误:
The LINQ expression 'DbSet<PlaceSync>
.LeftJoin(
outer: DbSet<PlaceIdentifier>
.Join(
outer: DbSet<PlaceSource>,
inner: p0 => p0.PlaceSourceId,
outerKeySelector: p1 => p1.PlaceSourceId,
innerKeySelector: (p0, p1) => new TransparentIdentifier<PlaceIdentifier, PlaceSource>(
Outer = p0,
Inner = p1
)),
inner: p => new {
PlaceKey = p.PlaceKey,
PlaceSource = p.PlaceSource.ToLower()
},
outerKeySelector: ti => new {
PlaceKey = ti.Outer.PlaceIdentifierKey,
PlaceSource = ti.Inner.PlaceSourceName.ToLower()
},
innerKeySelector: (p, ti) => new TransparentIdentifier<PlaceSync, TransparentIdentifier<PlaceIdentifier, PlaceSource>>(
Outer = p,
Inner = ti
))
.Where(ti0 => new {
Id = ti0.Inner.Outer.PlaceIdentifierId,
PlaceKey = ti0.Inner.Outer.PlaceIdentifierKey,
PlaceSource = ti0.Inner.Inner.PlaceSourceName
} == null)' 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.
LINQ表达式“DbSet”
.LeftJoin(
外部:DbSet
.加入(
外部:DbSet,
内部:p0=>p0.PlaceSourceId,
outerKeySelector:p1=>p1.PlaceSourceId,
innerKeySelector:(p0,p1)=>新的透明标识符(
外部=p0,
内部=p1
)),
内部:p=>new{
PlaceKey=p.PlaceKey,
PlaceSource=p.PlaceSource.ToLower()
},
outerKeySelector:ti=>new{
PlaceKey=ti.Outer.PlaceIdentifierKey,
PlaceSource=ti.Inner.PlaceSourceName.ToLower()
},
innerKeySelector:(p,ti)=>新的透明标识符(
外部=p,
内部=钛
))
.Where(ti0=>new{
Id=ti0.Inner.Outer.PlaceIdentifierId,
PlaceKey=ti0.Inner.Outer.PlaceIdentifierKey,
PlaceSource=ti0.Inner.Inner.PlaceSourceName
}==null)“”无法转换。以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算。看见https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。
我正在将.netcore与Micrisoft.EntityFrameworkCore版本3.1.5一起使用。
在这个版本的ef中是否缺少一些东西会阻止查询被正确翻译
如果您能提供一个在框架中工作的同一查询的示例,那就太好了。EG Core 3.x查询转换器无法翻译的表达式是反连接条件
其中c==null
事实上,这样的表达式没有SQL等价物-在SQL中执行左外部反联接时,将使用右侧的某个列,该列通常不可为NULL(通常为PK),用于is NULL
检查
这也可以用作LINQ查询中的变通方法,但与SQL不同,SQL自然支持任何表达式为null(即使对于不可为null的列),C#表达式需要显式转换为相应的可为null的类型(并忽略关联警告)
例如,如果aa.PlaceIdentifierId
的类型为int
,则标准为
其中(int?)c.Id==null
例如Core 3.x查询转换器无法翻译的表达式是反连接条件
其中c==null
事实上,这样的表达式没有SQL等价物-在SQL中执行左外部反联接时,将使用右侧的某个列,该列通常不可为NULL(通常为PK),用于is NULL
检查
这也可以用作LINQ查询中的变通方法,但与SQL不同,SQL自然支持任何表达式为null(即使对于不可为null的列),C#表达式需要显式转换为相应的可为null的类型(并忽略关联警告)
例如,如果aa.PlaceIdentifierId
的类型为int
,则标准为
其中(int?)c.Id==null
最后我选择了.FromSqlRaw…最后我选择了.FromSqlRaw…100%是左连接。当我移除它时,翻译会完美地进行。我读到,当导航属性丢失时,EF3.1.5中有一个突破性的变化,但事实并非如此;我在这里帮不了我。PlaceSyncs表与其他表完全无关。那么,您究竟如何构造这种类型的查询呢?100%是左连接。当我移除它时,翻译会完美地进行。我读到,当导航属性丢失时,EF3.1.5中有一个突破性的变化,但事实并非如此;我在这里帮不了我。PlaceSyncs表与其他表完全无关。那么,您究竟如何构造这种类型的查询呢?事实并非如此。当我试图以任何方式使用c时,我都会得到同样的错误。例如,如果我删除where子句并简单地放入SELECTNEW{c},我也会收到相同的错误。temp的默认值为null。如果您试图以您所述的方式编写表达式,您将得到Id不是null属性(或类似的内容)。不过谢谢你。。。(考虑到限制,你怎么做呢?)。当按照我建议的方式修改时,它会产生编译器警告,但会正确地翻译和执行。当然,这是由当前EF核心限制/缺陷造成的,但解决方法可行。回答具体问题的问答网站也是如此。上述内容解决了您问题中的具体问题。如果您试图以不同的方式使用c
变量,那么这是不同的问题。顺便说一句,在翻译后的查询中没有“object variable的值为null”这样的东西-这不是Linq to Objects,没有Objects。。。。。。完全涉及,但纯粹是SQL构造。正如我在文章中所写,这些“变量”不存在,因此不能检查null
。通常EF会在内部将这些检查转换为可为空的PKs,就像这里一样,只是目前在某些情况下(比如您的情况下)无法做到这一点。只需从原始查询中删除where
子句,或使用建议的更改即可证明这一点。。。谢谢事实并非如此。当我试图以任何方式使用c时,我都会得到同样的错误。例如,如果我删除where子句并简单地放入SELECTNEW{c},我也会收到相同的错误。temp的默认值为null。如果您试图以您所使用的方式编写表达式