ASP.NET和EF非常慢

Maz*_*aie 3 c# asp.net entity-framework

通过使用EF,C#和ASP.NET 4 Web应用程序,我使用以下代码从数据库中检索数据并填充GridView:

using (AshModel am = this.conn.GetContext())
{
    IEnumerable<Article> articles = 
        (from a in am.Article.AsEnumerable()
         where (a.CultureName == calture || a.CultureName == 0)
             && a.IsApproved == status
             && a.IsPublished == status
         orderby a.AddedDate descending
         select a);

    IEnumerable<Profile> profiles = am.Profile.AsEnumerable()
        .Where(t => articles.Any(a => a.ProfileId == t.ProfileID));

    foreach (Article article in articles)
        article.UserProfile = profiles
            .Where(a => a.ProfileID == article.ProfileId)
            .FirstOrDefault();

    this.gvArticles.DataSource = articles.ToList();
    this.gvArticles.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

但它非常慢,响应大约需要2分钟,数据库中只有500条记录!我的错误是什么?我如何才能提高绩效?谢谢.

Gus*_*man 11

AsEnumerable()在某些方面做的.

执行此操作时,将从数据库中检索所有对象,然后对其进行过滤.

如果你删除AsEnumerable()它应该按预期工作.