none
AEF里如何优化这个查询? RRS feed

  • 问题

  • var articles = db.ArticleUsers.FirstOrDefault(u => u.UserName == username).Articles;
                articles.Load();

                return articles.FirstOrDefault(a => a.ArticleId == id);

    说明 Articles  ArticleUsers 是AEF对应的2表,Articles 中有 ArticleUsers 的主键,也就是主外键关系,
    article.Load(); 应该是连接数据库并执行查询,这样的话查出来一个较大的集合,而我只是想要1条数据。
    2010年7月14日 9:38

答案

  • var articles = from articlesUser in db.ArticleUsers where articlesUser.UserName==userName && articlesUser.Articles.ArticleId==id

                        select articlesUser.Articles

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年7月14日 15:05
    版主

全部回复

  • var articles = from articlesUser in db.ArticleUsers where articlesUser.UserName==userName && articlesUser.Articles.ArticleId==id

                        select articlesUser.Articles

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年7月14日 15:05
    版主
  • 这样查询出来 articleUser.Articles 是个集合?

    若需单条数据是否:articles.First();?

    2010年7月15日 9:33
  • 是啊         

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年7月15日 14:15
    版主