How do I Convert [AnonymousType#1] or pre define it?

I get this Error

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<object>'

Running 3.5 .net Never had this issue before. not sure what was changed.

the error happens on line select new { ITEMID = p.PRESSRELEASEID };

i have tried but keep getting different convert errors.

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();
Jon Skeet
people
quotationmark

Three independent options:

  • Ditch the anonymous type entirely. You've only got a single value, so why not use select p.PRESSRELEASEID?
  • Cast in the select clause:

    select (object) new { ITEMID = p.PRESSRELEASEID }
    
  • Declare the variable using var, providing a dummy example:

    var pressreleases = new[] { new { ITEMID = "" } }.ToQueryable();
    

As an aside, I would strongly advise reconsidering your naming - both in terms of VERYSHOUTY names and veryquiet names; more conventional names would be pressReleases (for a local variable) and PressReleaseID (for a property).

people

See more on this question at Stackoverflow