I use an entity structure on a forum website and have two example queries with questions.
Query # 1) This query is supposed to get the number of topics by topics on the forum. Does the SQL account count so that I only get the number? Or does he pull all the topics into memory and then count the topics in the collection?
return DBContext.Topics
.Where(x => !x.ModsOnly)
.Where(x => !x.Board.ModsOnly)
.Where(x => !x.Board.Hidden)
.Count();
Query # 2) This query should receive all topics, order them by the date of the last response (or the date of the topic if there are no answers). He then calculates the results and prints the results. How many of this query are executed in the database? This request is accepted by FOREVER, so I think that at some point it will pull all the topics into memory, probably before the pagination affects.
var query = DBContext.Topics
.Where(x => !x.ModsOnly)
.Where(x => !x.Board.ModsOnly)
.Where(x => !x.Board.Hidden)
.OrderByDescending(x => x.GlobalSticky)
.ThenByDescending(x => x.Replies
.Where(r => !r.ModsOnly)
.Any() ? x.Replies
.Where(r => !r.ModsOnly)
.Max(r => r.PostedDate) : x.PostedDate);
int totalTopics = query.Count();
if (totalTopics > itemsPerPage)
return query.Skip((page - 1) * itemsPerPage).Take(itemsPerPage);
else
return query;
LINQ, . , , - , , / - , .
. "LastReplyDate" . , , , / .
, , , , , .