无法翻译 LINQ 表达式。要么以可以翻译的形式重写查询,要么显式切换到客户端评估
The LINQ expression could not not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly
我想维持child-grandchildren关系。我需要区分哪个 children 返回哪个 grandchildren.
查询returns
"InvalidOperationException: The LINQ expression
'x =>x.Children.AsQueryable().Select(c => new
{
ItemNo= c.ItemNo,
GrandChildItemNos = c.Children.AsQueryable().Select(gc => gc.ItemNo).ToList()
})
.ToDictionary(keySelector: c => c.ItemNo, elementSelector: c => c.GrandChildItemNos)' 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 'AsEnumerable',
'AsAsyncEnumerable','ToList', or 'ToListAsync'. See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information."
我认为 EF 在将 .ToDictionary() 部分翻译成 SQL 时遇到问题。谁能帮忙解决这个问题。
你很接近!
当您将 ToDictionary()
移动到实体化点(ToListAsync()
)之外时,它将起作用。这实际上也是错误消息所说的:“或显式切换到客户端评估”。 ToDictionary
调用无法转换为查询。
var childDetails = await context.Items.Where(w => w.ItemNo == parentItemNo)
.SelectMany(x => x.Children
.Select(c => new
{
c.ItemNo,
GrandChildItemNos = c.Children.Select(gc => gc.ItemNo)
})
)
.ToListAsync();
var dictionary = childDetails.ToDictionary(d => d.ItemNo, d => d.GrandChildItemNos);
这是我的结果:
我想维持child-grandchildren关系。我需要区分哪个 children 返回哪个 grandchildren.
查询returns
"InvalidOperationException: The LINQ expression
'x =>x.Children.AsQueryable().Select(c => new
{
ItemNo= c.ItemNo,
GrandChildItemNos = c.Children.AsQueryable().Select(gc => gc.ItemNo).ToList()
})
.ToDictionary(keySelector: c => c.ItemNo, elementSelector: c => c.GrandChildItemNos)' 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 'AsEnumerable',
'AsAsyncEnumerable','ToList', or 'ToListAsync'. See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information."
我认为 EF 在将 .ToDictionary() 部分翻译成 SQL 时遇到问题。谁能帮忙解决这个问题。
你很接近!
当您将 ToDictionary()
移动到实体化点(ToListAsync()
)之外时,它将起作用。这实际上也是错误消息所说的:“或显式切换到客户端评估”。 ToDictionary
调用无法转换为查询。
var childDetails = await context.Items.Where(w => w.ItemNo == parentItemNo)
.SelectMany(x => x.Children
.Select(c => new
{
c.ItemNo,
GrandChildItemNos = c.Children.Select(gc => gc.ItemNo)
})
)
.ToListAsync();
var dictionary = childDetails.ToDictionary(d => d.ItemNo, d => d.GrandChildItemNos);
这是我的结果: