How to turn this linq query to lazy loading

Posted by Luke101 on Stack Overflow See other posts from Stack Overflow or by Luke101
Published on 2010-06-08T06:04:08Z Indexed on 2010/06/08 6:12 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ