获取菜单时Linq查询错误 [英] Linq query error while fetching menus

查看:77
本文介绍了获取菜单时Linq查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在var中获取菜单列表但是当我尝试将其强制转换为正确的对象时它不起作用,



异常:下面的代码可以工作并给出菜单在$





 // var pageObject =(来自op in gc.Menus 
/ / join mr in gc.MenuRights on op.MenuId equals mr.MenuId
//其中mr.LevelId == LevelId
//选择新的{op});







下面的代码不起作用并给出错误,不能隐式转换类型

 System.collections.Generic.List<<匿名类型:Geem.Models.Menu>> to System.collections.Generic.List< Geem.Models.Menu> 





我尝试过:



列表<菜单> mList =(来自op in gc.Menus 
join mr in gc.MenuRights on op.MenuId equals mr.MenuId
where mr.LevelId == LevelId
select new {op})。ToList ();

解决方案

问题是你在使用匿名类型(新{...})必须。匿名类型有各种限制和怪癖,所以除非你真的需要,否则不要使用它们。



这是相同的代码而不使用匿名类型



列表<菜单> mList =(来自op in gc.Menus 
join mr in gc.MenuRights on op.MenuId equals mr.MenuId
where mr.LevelId == LevelId
select op).ToList();


I am able to get the menu list in var but when i try to cast it to correct Object it doesnt work,

Exception: Below code works and gives menus in var


//var pageObject = (from op in gc.Menus
           //                  join mr in gc.MenuRights on op.MenuId equals mr.MenuId
           //                  where mr.LevelId == LevelId
           //                  select new { op });




Below code does not work and gives error as,Cannot implicitly convert type

System.collections.Generic.List<<anonymous type: Geem.Models.Menu>> to System.collections.Generic.List<Geem.Models.Menu>



What I have tried:

List<Menu> mList = (from op in gc.Menus
          join mr in gc.MenuRights on op.MenuId equals mr.MenuId
          where mr.LevelId == LevelId
          select new { op }).ToList();

解决方案

The problem is that you're using anonymous types (new { ... }) when you don't have to. Anonymous types have various restrictions and quirks so don't use them unless you really need to.

This is the same code without the use of anonymous types

List<Menu> mList = (from op in gc.Menus
          join mr in gc.MenuRights on op.MenuId equals mr.MenuId
          where mr.LevelId == LevelId
          select op).ToList();


这篇关于获取菜单时Linq查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆