Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting iQueryable to IEnumerable

What is the problem with my code below? It's not returning any items even when matching records are present in the database. If it's wrong, how can I convert my IQueryable to IEnumerable?

public IEnumerable<TimelineItem> TimeLineItems { get; set; }
public IEnumerable<TimelineItem> GetTimeLineItems(int SelectedPID)
{
    TimeLineItems = (from t in db.TimelineItems
                     where t.ProductID == SelectedPID
                     select new { t.Description, t.Title }) as IEnumerable<TimelineItem>;
    return TimeLineItems;
}
like image 983
Renny Avatar asked Apr 11 '12 01:04

Renny


People also ask

Which is faster IEnumerable or IQueryable?

IQueryable is faster than IEnumerable. In addition to Munesh Sharma's answer:IEnumerable loads data in-memory and then apply filters to it one by one but IQueryable apply filters all at once and return the result.

What is the difference between IEnumerable and IQueryable?

The major difference between IQueryable and IEnumerable is that IQueryable executes query with filters whereas IEnumerable executes the query first and then it filters the data based on conditions.

Does IQueryable implement IEnumerable?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed.


1 Answers

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>();
   }
like image 66
Travis J Avatar answered Oct 13 '22 16:10

Travis J