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;
}
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.
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.
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.
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>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With