Converting iQueryable to IEnumerable

What is your business use case for converting the IQueryable to IEnumerable? An IQueryable will give you deferred or lazy, non-cached data whereas the IEnumerable will give you immediate or eager, cached data. However, if you have lazy loading turned off at the context level, you'll still get immediate data.


The reason you are getting null is because you are trying to convert an IQueryable based on an an anonymous type to IEnumerable<TimelineItem> (new { t.Description, t.Title } creates an instance of an anonymous type with two fields - Description and Title) You should remove the Select part to make it work.

If you would like to select only Description and Title, create a named type with these two fields, and return an IEnumerable of that type:

public class TitleDescr {
    public string Title {get;set;}
    public string Description {get;set;}
}

public IEnumerable<TitleDescr> GetTimeLineItems(int SelectedPID)
{
    return from t in db.TimelineItems
                     where t.ProductID == SelectedPID
                     select new TitleDescr { t.Description, t.Title };
}

In my opinion, if you are going to use linq then embrace it, get rid of that esoteric notation :)

   public IEnumerable<TimelineItem> GetTimeLineItems(int SelectedPID)
   {
      return db.TimelineItems.Where(tl => tl.ProductID == SelectedPID)
        .Select( tl => new TimelineItem {
            Description = tl.Description,
            Title = tl.Title })
        .AsEnumerable<TimelineItem>();
   }