Reputation: 381
I have such a case study:
ToList()
case:
List<CategoryType> categories = (from c in categoryTypes where c.IsSysParam == isSysParamCategory select new CategoryType { Code = c.Code, CreateDate = c.CreateDate, EditDate = c.EditDate, IsProductCategory = c.IsProductCategory, IsSysParam = c.IsSysParam, Name = c.Name, TypeId = c.TypeId, ValueTypes = new List<ValueType>() }).ToList();
List<ValueType> valueTypeList = new List<ValueType>();
foreach (var c in categories.ToList())
{
valueTypeList = categoryTypes.Where(x => x.TypeId == c.TypeId).SelectMany(v => v.ValueTypes).Where(v => v.ParentValueId == null).ToList();
c.ValueTypes = valueTypeList;
}
IQueryable
case:
When I change in first query - List<CategoryType>
to IQueryable<CategoryType>
and remove ToList()
from the end of query then I dont have any result:
Question:
I am asking for an explanation, I do not understand why this is happening. I know that the IQueryable
makes some part of the work on the database side.
Edit: The code is working, pictures shows the final effect.
I have:
public IQueryable<CategoryType> CategoryTypePagination { get; set; }
and in the end of ToList()
case:
this.CategoryTypePagination = categories.AsQueryable();
in IQueryable
case just removed .AsQueryable()
Upvotes: 0
Views: 257
Reputation: 37337
Accrodingly to this, IQueryable
uses something called lazy loading.
So the results of IQueryable
aren't loaded until they are first used, for example in Sum
, ToList
or ToArray
methods, while ToList
requieres data to be loaded. Thus you see the difference after initailizing both objects.
Upvotes: 0
Reputation: 382
You have to look at "Deferred Query Execution" and "Immediate Query Execution"
Upvotes: 1