0

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; } 

enter image description here

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:

enter image description here

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

3
  • 1
    Post the actual code, not screenshots and descriptions. The code and screenshots don't match. The object definitions are missing. You claim you were able to assign an IQueryable to a List<T> variable without compilation errors? Commented Sep 6, 2018 at 8:44
  • I think this link is much more helpfull than my explanation attempts Commented Sep 6, 2018 at 8:46
  • Panagiotis Kanavos - I edited post. Commented Sep 6, 2018 at 9:00

2 Answers 2

1

You have to look at "Deferred Query Execution" and "Immediate Query Execution"

Sign up to request clarification or add additional context in comments.

1 Comment

The code sets the ValueTypes property directly - even though in the second "snippets" the value is supposed to be an IQueyrable that just can't be assigned to a List<T>
0

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.

3 Comments

It's more likely that the code doesn't even run. It's trying to assign an IQueryable to a List<T> variable. This should throw
@PanagiotisKanavos No, OP stated that he changed the type to IQueryable and removed ToList() method. As can be also seen in [pictures, code runs well.
The screenshots don't match the code. They are from a different snippet entirely. The OP doesn't explain what changes were made - IQueryable doesn't have a .Count property. If ValueTypes is still a list, how was an IQueryable assigned to it? And since ValueTypes contains the query's root results its count wouldn't be affected by lazy loading

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.