The scope of this query, it's display a navigation menu grouped by Category > Main menus > Submenus.
In LINQPad works fine, but the web application shows me the following unhandled error:
InvalidOperationException: Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are required to generate results on client side. This can happen when trying to correlate on keyless entity or when using 'Distinct' or 'GroupBy' operations without projecting all of the key columns.
Query:
var mainMenu = (from c in _unitOfWork.Menu.GetAllQueryable()
where c.MenuPadreId == 0
select new
{
Categoria = c.MenuCategoria.Descripcion,
CategoriaId = c.MenuCategoriaId
}).Distinct();
var menuNavegacion = from c in mainMenu
select new MenuNavegacionCategoriaViewModel
{
Categoria = c.Categoria,
CategoriaId = c.CategoriaId,
MenuNavegacionViewModel = (from m in _unitOfWork.Menu.GetAllQueryable()
where m.MenuPadreId == 0
&& m.MenuCategoriaId == c.CategoriaId
select new MenuNavegacionViewModel
{
MenuId = m.MenuId,
MenuPadreId = m.MenuPadreId,
Descripcion = m.Descripcion,
Area = m.Area,
Controlador = m.Controlador,
Accion = m.Accion,
TieneUrl = m.TieneUrl,
CountSubMenu = _unitOfWork.Menu.GetAllQueryable().Count(me => me.MenuPadreId > 0 && me.MenuPadreId == m.MenuId),
Icono = m.Icono,
MenuNavegacionSubViewModel = (from s in _unitOfWork.Menu.GetAllQueryable()
where s.MenuPadreId > 0
&& s.MenuPadreId == m.MenuId
group s by new
{
MenuId = s.MenuId,
MenuPadreId = s.MenuPadreId,
Descripcion = s.Descripcion,
Area = s.Area,
Controlador = s.Controlador,
Accion = s.Accion,
TieneUrl = s.TieneUrl,
Icono = s.Icono
} into sjoin
select new MenuNavegacionSubViewModel
{
MenuId = sjoin.Key.MenuId,
MenuPadreId = sjoin.Key.MenuPadreId,
Descripcion = sjoin.Key.Descripcion,
Area = sjoin.Key.Area,
Controlador = sjoin.Key.Controlador,
Accion = sjoin.Key.Accion,
TieneUrl = sjoin.Key.TieneUrl,
Icono = sjoin.Key.Icono
}).OrderBy(mnsvm => mnsvm.Descripcion).ToList()
}).OrderBy(mnvm => mnvm.Descripcion).ToList()
};
ViewModels:
public class MenuNavegacionCategoriaViewModel
{
public string Categoria { get; set; }
public int CategoriaId { get; set; }
public List<MenuNavegacionViewModel> MenuNavegacionViewModel { get; set; }
}
public class MenuNavegacionViewModel
{
public int MenuId { get; set; }
public int MenuPadreId { get; set; }
public string Descripcion { get; set; }
public string Area { get; set; }
public string Controlador { get; set; }
public string Accion { get; set; }
public List<MenuNavegacionSubViewModel> MenuNavegacionSubViewModel { get; set; }
public bool TieneUrl { get; set; }
public int CountSubMenu { get; set; }
public string Icono { get; set; }
}
public class MenuNavegacionSubViewModel
{
public int MenuId { get; set; }
public int MenuPadreId { get; set; }
public string Descripcion { get; set; }
public string Area { get; set; }
public string Controlador { get; set; }
public string Accion { get; set; }
public bool TieneUrl { get; set; }
public string Icono { get; set; }
public string MenuPadreDescripcion { get; set; }
}
Group by throws me the same error.
One of your top-level projection ( MenuNavegacionCategoriaViewModel
for example) has not nullable column ( CategoriaId
let's say) as it selected from foreign key column. Make your CategoriaId
in MenuNavegacionCategoriaViewModel nullable and it will fly.
This problem is one of the breaking changes in EF Core 5.0. Some queries with correlated collection that also use Distinct or GroupBy are no longer supported. Read this for more information
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.