How to turn this linq query to lazy loading
- by Luke101
I would like to make a certain select item to lazy load latter in my linq query. Here is my query
var posts = from p in context.post
where p.post_isdeleted == false && p.post_parentid == null
select new
{
p.post_date,
p.post_id,
p.post_titleslug,
p.post_votecount,
FavoriteCount = context.PostVotes.Where(x => x.PostVote_postid == p.post_id).Count() //this should load latter
};
I have deleted the FavoriteCount item in the select query and would like it to ba added later based on certain conditions. Here is the way I have it lazy loaded
if (GetFavoriteInfo)
{
posts = posts.Select(x => new { FavoriteCount = context.PostVotes.Where(y => y.PostVote_postid == x.post_id).Count() });
}
I am getting a syntax error with this the above query. How do I fix this