日期:2014-05-20  浏览次数:20781 次

无法将类型“System.Linq.IQueryable<AnonymousType#1>”隐式转换问题
错误 22 无法将类型“System.Linq.IQueryable<AnonymousType#1>”隐式转换为“System.Linq.IQueryable<pms.shtip.com.cn.BIDepartment>”。存在一个显式转换(是否缺少强制转换?) C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\PMSEXTDLLOLD\PMSEXTDLLOLD\App_Code\BIDepartment.cs 167 47 C:\...\PMSEXTDLLOLD\

上述错误提示停在下列代码中的 select 上,但是如果我改为 select new BIDepartment 又提示“不允许在查询中显式构造实体类型”
我改如何才能获得 IQueryable<BIDepartment> 类型的result呢?

IQueryable<BIDepartment> result = from c in db.BIDepartment
  select new 
  {
  AddDate = c.AddDate,
  AddUserId = c.AddUserId,
  DepartmentName = c.DepartmentName,
  DSortId = c.DSortId,
  DType = c.DType,
  F1 = c.F1,
  F2 = c.F2,
  F3 = c.F3,
  FeedBack = c.FeedBack,
  Id = c.Id,
  Important = c.Important,
  InnerCode = c.InnerCode,
  Intro = c.Intro,
  IsDel = c.IsDel,
  IsSys = c.IsSys,
  LastModifyDate = c.LastModifyDate,
  LastModifyUserId = c.LastModifyUserId,
  Orders = c.Orders,
  ParentBIDepartmentName = c.ParentBIDepartmentName,
  ParentId = c.ParentId,
  State = c.State,
  SubCompanyId = c.SubCompanyId,
  TreePath = GetTreePath(c.Id, ""),
  UserCode = c.UserCode
  }
  ;
 

------解决方案--------------------
IQueryable<BIDepartment> result=new IQueryable<<BIDepartment>();
var n=db.BIDepartment.select(p=>p);
foreach(var c in n)
{
result.add(new BIDepartment
{
AddDate = c.AddDate, 
AddUserId = c.AddUserId, 
DepartmentName = c.DepartmentName, 
DSortId = c.DSortId, 
DType = c.DType, 
F1 = c.F1, 
F2 = c.F2, 
F3 = c.F3, 
FeedBack = c.FeedBack, 
Id = c.Id, 
Important = c.Important, 
InnerCode = c.InnerCode, 
Intro = c.Intro, 
IsDel = c.IsDel, 
IsSys = c.IsSys, 
LastModifyDate = c.LastModifyDate, 
LastModifyUserId = c.LastModifyUserId, 
Orders = c.Orders, 
ParentBIDepartmentName = c.ParentBIDepartmentName, 
ParentId = c.ParentId, 
State = c.State, 
SubCompanyId = c.SubCompanyId, 
TreePath = GetTreePath(c.Id, ""), 
UserCode = c.UserCode 
}
);
}
return result;


如果你返回的是IQueryable<>集合的话,我想,你应该这样写