如何在下一个方法中将IQueryable作为参数?

时间:2016-08-06 21:38:14

标签: c# asp.net-mvc entity-framework

我会尝试让我的代码更好,我不知道如何让IQueryable到下一个功能

错误:

  

发生了'System.InvalidOperationException'类型的异常   EntityFramework.dll但未在用户代码中处理

     

附加信息:由于操作无法完成   DbContext已被处置。

这是问题:

public List<GitUser> FavoritesUsersListFromHistory(IQueryable users)
    {
        List<GitUser> favoritesList = new List<GitUser>();
        using (var db = new GitContext())
        {
            foreach (CookiesHistory result in users) <-- users make this error
            {
                var user = (from u in db.GitUsers
                            where u.Id == result.GitUserId
                            select new { u }).First();

                favoritesList.Add(user.u);
            }
        }
        return favoritesList;

这是更改前的代码:

            List<GitUser> favoritesList = new List<GitUser>();


        using (var db = new GitContext())
        {
            var results = (from ch in db.CookiesHistory
                           where ch.GitUserId != null
                           where ch.MyCookieId == cookieId
                           group ch by new { ch.GitUserId, ch.MyCookieId, ch.SearchGitUser } into g
                           orderby g.Count() descending
                           select new { GitUserId = g.Key.GitUserId, MyCookieId = g.Key.MyCookieId, SearchGitUser = g.Key.SearchGitUser, Count = g.Count() }).Take(count);


            foreach (var result in results)
            {
                var user = (from u in db.GitUsers
                            where u.Id == result.GitUserId
                            select new { u }).First();

                favoritesList.Add(user.u);
            }

            return favoritesList;

这是更改后的代码:

 public FavoritesController(IGitUserRepository repo,  CookieHelper _cookieHelper)
    {
        //myCookieRepository = cookieRepo;IMyCookieRepository cookieRepo,
        repository = repo;
        cookieHelper = _cookieHelper;
    }




    // GET: Favorites
    public PartialViewResult FavoritesRepos()
    {
        IEnumerable<GitUser> favorites = FavoritesUsers(cookieHelper.GetHttpCookieId(), countOfFavoritesUsers);
        return PartialView("_FavoritesRepos", favorites);
    }



    public IEnumerable<GitUser> FavoritesUsers(int cookieId, int count)
    {
            return FavoritesUsersListFromHistory(TopFavoritesUsers(cookieId, count));
    }

    public IQueryable TopFavoritesUsers(int cookieId, int count)
    {
        using (var db = new GitContext())
        {
            IQueryable results = (from ch in db.CookiesHistory
                           where ch.GitUserId != null
                           where ch.MyCookieId == cookieId
                           group ch by new { ch.GitUserId, ch.MyCookieId, ch.SearchGitUser } into g
                           orderby g.Count() descending
                           select new { GitUserId = g.Key.GitUserId, MyCookieId = g.Key.MyCookieId, SearchGitUser = g.Key.SearchGitUser, Count = g.Count() }).Take(count);
            return results;
        }
    }

    public List<GitUser> FavoritesUsersListFromHistory(IQueryable users)
    {
        List<GitUser> favoritesList = new List<GitUser>();
        using (var db = new GitContext())
        {
            foreach (CookiesHistory result in users)
            {
                var user = (from u in db.GitUsers
                            where u.Id == result.GitUserId
                            select new { u }).First();

                favoritesList.Add(user.u);
            }
        }
        return favoritesList;
    }

1 个答案:

答案 0 :(得分:1)

首先,错误是由于您在创建要传递的查询时使用的上下文,因为在您尝试迭代结果时已经处理了参数。所以,我的第一个建议是将您的上下文变量创建为全局变量:

public class FavoriteController
{
   protected GitContext db;
   public FavoriteController()
   {
     db=new GitContext();
   }

  public IEnumerable<GitUser> FavoritesUsers(int cookieId, int count)
  {
        return FavoritesUsersListFromHistory(TopFavoritesUsers(cookieId, count));
  }

  //create a class (CookiesHistoryDTO) to save the result of this query
  public IQueryable<CookiesHistoryDTO> TopFavoritesUsers(int cookieId, int count)
  {

        IQueryable<CookiesHistoryDTO> results = (from ch in db.CookiesHistory
                                               where ch.GitUserId != null
                                               where ch.MyCookieId == cookieId
                                               group ch by new { ch.GitUserId, ch.MyCookieId, ch.SearchGitUser } into g
                                               orderby g.Count() descending
                                               select new CookiesHistoryDTO{ GitUserId = g.Key.GitUserId, MyCookieId = g.Key.MyCookieId, SearchGitUser = g.Key.SearchGitUser, Count = g.Count() }
                                              ).Take(count);
        return results;

  }


  public IEnumerable<GitUser> FavoritesUsersListFromHistory(IQueryable<CookiesHistoryDTO> user)
  {
     //Here the context you use to create your first query is still alive
     var userIds=user.Select(e=>e.GitUserId).Contains(e.Id);
     return db.GitUsers.Where(e=>userIds.Contains(e.Id)).ToList();// Here is when both query are going to be executed.
  }

  protected override void Dispose(bool disposing)
  {
    if (disposing)
    {
        db.Dispose();
    }
    base.Dispose(disposing);
  }
}