错误:无法隐式转换为System.linq.iqueryable< System.Linq.IGrouping< string,AnonymousType#1>> to system.Collection.List<>,并且显式转换存在..你错过了演员吗? [英] Error: Cannot Implicitly Convert to To System.linq.iqueryable<System.Linq.IGrouping<string,AnonymousType#1>> to system.Collection.List<>, and explicit conversion exist..are you missing a cast ?
本文介绍了错误:无法隐式转换为System.linq.iqueryable< System.Linq.IGrouping< string,AnonymousType#1>> to system.Collection.List<>,并且显式转换存在..你错过了演员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对asp.net不是很熟悉。简要解释为什么em遇到这个错误。
这是我的代码
public List< Department> getDeptNames(){
使用(
RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities())
{
var obj = context.Departments.ToList();
列表<部门> dic =( from p in context.Departments
选择 new {p.DepartmentId,p.DepartmentName})。GroupBy(o = > o.DepartmentName);
return obj;
}}
解决方案
假设您的部门课程是什么
public class department
{
public int DepartmentId { get ; set ; }
public string DepartmentName { get 跨度>; set ; }
}
然后你需要像这样编写你的查询... < br $> b $ b
public 列表< Department> getDeptNames()
{
使用(RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities())
{
List< Department> dic =( from p in context.Departments
选择 new 部门
{
DepartmentId = p.DepartmentId,
DepartmentName = p.DepartmentName
}
).ToList();
return obj;
}
}
(来自上下文中的p.Departments
select new {p。 DepartmentId,p.DepartmentName})。GroupBy(o => o.DepartmentName);
您选择匿名
类型,因此无法将其强制转换为列表< department>< / department>
尝试:
(来自 p context.Departments
select p)。 GroupBy(o = > o.DepartmentName)。ToList();
试试这个< br $> b $ b
列表< department> dic =(List< department>)( from p in context.Departments
选择 new {p.DepartmentId,p.DepartmentName})。GroupBy(o = > o.DepartmentName); < / department > < / department > 跨度>
hy i am not very much familiar to asp.net . Explain briefly why em encountering this error.
here is my code
public List<Department> getDeptNames(){
using (
RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities())
{
var obj = context.Departments.ToList();
List<Department> dic = (from p in context.Departments
select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);
return obj;
}}
解决方案
suppose your Department class is something
public class department { public int DepartmentId { get; set; } public string DepartmentName { get; set; } }
then you need to write your query like this...
public List<Department> getDeptNames() { using (RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities()) { List<Department> dic = (from p in context.Departments select new Department { DepartmentId = p.DepartmentId, DepartmentName = p.DepartmentName } ).ToList(); return obj; } }
(from p in context.Departments select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);
You are selecting anAnonymous
type, hence will not be able to cast it toList<department></department>
Try:
(from p in context.Departments select p).GroupBy( o=> o.DepartmentName).ToList();
Try this
List<department> dic =(List<department>) (from p in context.Departments select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);</department></department>
这篇关于错误:无法隐式转换为System.linq.iqueryable< System.Linq.IGrouping< string,AnonymousType#1>> to system.Collection.List<>,并且显式转换存在..你错过了演员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文