0
votes
var test1 = DB.UnAuthPriorityLevelTypes.AsEnumerable();
var test2 = DB.UnAuthPriorityLevelTypes.AsQueryable();
var test3 = DB.UnAuthPriorityLevelTypes.ToList();
var test4 = DB.UnAuthPriorityLevelTypes.AsEnumerable();
var test5 = DB.UnAuthPriorityLevelTypes.AsQueryable();

In the above five statements, if I look at the count of values in test1 and test2 it's 0, but test3, test4, test5 have the count value of 3, which was supposed to be the case. Why?

Update:

Even though test1 shows as 0 results , i m able to loop through it successfully and get the values I thought the problem is due to Lazy Loading behaviour of EF but i tried to disable it and ran it but no go

 if(DB.Configuration.LazyLoadingEnabled)
                DB.Configuration.LazyLoadingEnabled = false;
1
What kind of database is this ? How did you look at 'count of values' ? - Ako
This is SQL server Database. I m seeing the count while doing the debugging. And as I stated, while doing the iteration through that Ienumerable variable, I could successfully loop through it. - TechQuery

1 Answers

0
votes

Because of deferred execution of Linq, you should read AsEnumerable and AsQueryable as a declaration for the query to execute.

Once you run ToList you enforce an execution of the query and the datacontext will contain all the rows from DB.UnAuthPriorityLevelTypes and therefore Count will show the correct number and this will not change as long as the context is not disposed.

LazyLoadingEnabled flag influences related related rows, see this MSDN article.