'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

I want to maintain child-grandchildren relationship. I need to distinguish which children returned which grandchildren.

Query 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."

I think EF has problem translating .ToDictionary() part to SQL. Can anyone help on this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source