How to optimize this Linq query

Posted by Luke101 on Stack Overflow See other posts from Stack Overflow or by Luke101
Published on 2010-06-02T05:59:04Z Indexed on 2010/06/02 6:03 UTC
Read the original article Hit count: 243

Filed under:
|

I am trying to optimize this query. It is not slow for now but I am expecting a spike to hit this query in the near future. Is there anything else i can do to make this query faster?

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_title,
                            p.post_descriptionrender,
                            p.userinfo.user_username,
                            p.userinfo.user_userid,
                            p.userinfo.user_GravatarHash,
                            p.userinfo.user_points,
                            p.category.catid,
                            p.category.name,
                            p.post_answercount,
                            p.post_hasbestanswer,
                            p.post_hits,
                            p.post_isanonymous,
                            p.post_votecount,
                            FavoriteCount = context.favorites.Where(x => x.post.post_id == p.post_id).Count(),
                            tags = from tg in context.posttag
                                   where tg.posttag_postid == p.post_id
                                    select new
                                    {
                                        tg.tag.tag_id,
                                        tg.tag.tag_title
                                    }
                        };

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-entities