简体   繁体   中英

IQueryable vs ICollection (List)

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM