我收到此错误
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<object>'
运行3.5 .net之前从未遇到此问题。不知道改变了什么。
错误发生在选择新{ITEMID = p.PRESSRELEASEID};
我已经尝试过,但仍然会遇到不同的转换错误。
StringBuilder textStr = new StringBuilder();
var years = (from ys in db.tblPressReleases orderby ys.prdate descending select ys.prdate.Year).Distinct();
ddlYear.DataSource = years;
ddlYear.DataBind();
ddlYear.Items.Insert(0, new ListItem("Year", ""));
IQueryable<object> pressreleases ;
DateTime startdate, enddate;
if (this.iFilterMonth != null && this.iFilterMonth > 0 && this.iFilterYear != null)
{
startdate = new DateTime((int)this.iFilterYear, (int)this.iFilterMonth, 1);
enddate = startdate.AddMonths(1);
pressreleases = from p in db.tblPressReleases
where p.prdate.Date >= startdate.Date && p.prdate.Date < enddate.Date
orderby p.prdate descending
select new { ITEMID = p.PRESSRELEASEID };
}
else if (this.iFilterYear != null)
{
startdate = new DateTime((int)this.iFilterYear, 1, 1);
enddate = new DateTime((int)this.iFilterYear + 1, 1, 1);
pressreleases = from p in db.tblPressReleases
where p.prdate.Date >= startdate.Date && p.prdate.Date < enddate.Date
orderby p.prdate descending
select new { ITEMID = p.PRESSRELEASEID };
}
else
{
pressreleases = (from p in db.tblPressReleases
orderby p.prdate descending
select new { ITEMID = p.PRESSRELEASEID }).Take(15);
}
dlDisplay.DataSourceID = "";
dlDisplay.DataSource = pressreleases;
dlDisplay.Visible = true;
dlDisplay.DataBind();
答案 0 :(得分:2)
三个独立选项:
select p.PRESSRELEASEID
?转换为select
子句:
select (object) new { ITEMID = p.PRESSRELEASEID }
使用var
声明变量,提供一个虚拟示例:
var pressreleases = new[] { new { ITEMID = "" } }.ToQueryable();
顺便说一句,我强烈建议重新考虑你的命名 - 无论是在VERYSHOUTY名字还是非常安静的名字方面;更常规的名称是pressReleases
(对于局部变量)和PressReleaseID
(对于属性)。