public ActionResult Index(int? p, FormCollection formcollection)
{
var Query1 = from CompanyInfo in ef.Company_Info orderby CompanyInfo.Company_ID descending select new { AutoID=CompanyInfo.AutoID, CompanyAddress=CompanyInfo.CompanyAddress, CompanyName=CompanyInfo.CompanyName, CompanyTel=CompanyInfo.CompanyTel };
PagedList<Company_Info> QueryList = Query1.ToPagedList(p ?? 1, 20);
return View(QueryList);
}
一直报错:
错误 1 无法将类型“System.Linq.IQueryable<AnonymousType#1>”隐式转换为“System.Linq.IOrderedQueryable<PingL.Models.Company_Info>”。存在一个显式转换(是否缺少强制转换?) E:\pingl\PingL\PingL\Controllers\HomeController.cs 28 245 PingL
------最佳解决方案--------------------------------------------------------
那就用2、3楼版主说的试试,不过我记得获取实体类型时不能用这种投影方法,会出现错误,你试一下,没有问题最好。
------其他解决方案--------------------------------------------------------
select CompanyInfo 就行了,干吗要再转换成匿名对象?你用select new { AutoID=CompanyInfo.AutoID, CompanyAddress=CompanyInfo.CompanyAddress, CompanyName=CompanyInfo.CompanyName, CompanyTel=CompanyInfo.CompanyTel }这个出来的就不是Company_Info对象了,当然会有类型错误的问题
------其他解决方案--------------------------------------------------------
select new { ...
=>
select new CompanyInfo() { ...
------其他解决方案--------------------------------------------------------
++
var Query1 = from CompanyInfo in ef.Company_Info orderby CompanyInfo.Company_ID descending select new Company_Info{ AutoID=CompanyInfo.AutoID, CompanyAddress=CompanyInfo.CompanyAddress, CompanyName=CompanyInfo.CompanyName, CompanyTel=CompanyInfo.CompanyTel };
------其他解决方案--------------------------------------------------------
因为我表的字段比较多有10几,但现在这页只需要搜索出3就字段,如果是使用匿名对象会不会速度比较快,性能比较好,不需要把全部字段都输出一遍