Linq 如何将多次函数调用的查询结果与实体框架5结合起来?

Linq 如何将多次函数调用的查询结果与实体框架5结合起来?,linq,entity-framework-5,Linq,Entity Framework 5,我想使用一个函数来查询数据库与linq和结合他们的结果,我写的代码如下,但无法工作,有人可以帮我吗?谢谢 错误:(操作无法完成,因为DbContext已被释放) 零件代码: public static IEnumerable<UserLocation> loadedUserList; public static IEnumerable<UserLocation> combinedUserList; public static void loadDataInStati

我想使用一个函数来查询数据库与linq和结合他们的结果,我写的代码如下,但无法工作,有人可以帮我吗?谢谢

错误:(操作无法完成,因为DbContext已被释放)

零件代码:

public static IEnumerable<UserLocation> loadedUserList;

public static IEnumerable<UserLocation> combinedUserList;


public static void loadDataInStaticClass()
            {
                using (var context = new SptialTestEntities())
                {
                    var loadedUserList = from newRcords in context.UserLocations
                                         where newRcords.ID > lastLoadedID
                                         select newRcords;

                    if (loadedUserList.Any())
                    {
                        foreach (var user in loadedUserList)
                        {
                            Console.WriteLine(user.UserName);

                        }
                        if (combinedUserList != null)
                        {
                            combinedUserList = loadedUserList.Union(combinedUserList);
                            foreach (var cc in combinedUserList)
                            {
                                Console.WriteLine("\n after combined:" + cc.UserName);
                            }
                        }
                        else
                        {
                            combinedUserList = loadedUserList;
                            Console.WriteLine("\nfirst run :" + combinedUserList.Count());
                            foreach (var cc in combinedUserList)
                            {
                                Console.WriteLine("\n user:" + cc.UserName);
                            }

                        }
                    }

                }


            }
公共静态IEnumerable loadedUserList;
公共静态IEnumerable组合服务列表;
公共静态void loadDataInstanceClass()
{
使用(var context=new sptialestantities())
{
var loadedUserList=来自context.UserLocations中的newRcords
其中newRcords.ID>lastLoadedID
选择新单词;
if(loadedUserList.Any())
{
foreach(loadedUserList中的var用户)
{
Console.WriteLine(user.UserName);
}
if(combinedUserList!=null)
{
combinedUserList=loadedUserList.Union(combinedUserList);
foreach(组合服务器列表中的var cc)
{
Console.WriteLine(“\n组合后:“+cc.UserName”);
}
}
其他的
{
combinedUserList=loadedUserList;
Console.WriteLine(“\n第一次运行:+combinedUserList.Count());
foreach(组合服务器列表中的var cc)
{
Console.WriteLine(“\n用户:“+cc.UserName”);
}
}
}
}
}
问题是:第一个调用正常,但第二个报告错误:操作无法完成,因为DbContext已被释放,如何完成?
谢谢

我粘贴了整个代码,有人可以运行并检查错误,谢谢你: userLocation表包含userid、username、userLocation(地理类型)和 我在visual studio 2012中使用用户数据库第一种模式,并将userLocation映射到一个SptialTestEntities实体

Program.cs
static void Main(string[] args)
{

  for (int i = 1; i < 3; i++)
   {
      Console.WriteLine("\nrun{0}times, i);
      Test.LoadUsersFromDB();
   }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data.Spatial;
using System.Data.Entity;
using System.Xml.Linq;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections;
using System.Globalization;

namespace SptialMatch
{
    class Test
    {
        public static int lastedLoadedRecordID = 14;

        public static IEnumerable<UserLocation> loadedUserList;
        public static IEnumerable<UserLocation> combinedUserList;

        public static void LoadUsersFromDB()
        {
            try
            {
                Console.WriteLine("\n------------------------load data begin----------------------------------------------------------");

                //var context = new SptialTestEntities();
                using (var context = new SptialTestEntities())
                {
                    System.Diagnostics.Stopwatch loadStopwatch = new System.Diagnostics.Stopwatch();
                    loadStopwatch.Start();

                    loadedUserList = from newRcords in context.UserLocations
                                      where newRcords.ID > lastedLoadedRecordID
                                      select  newRcords;
                   if (loadedUserList.Any())
                    {
                        foreach (var loadUser in loadedUserList)
                        {
                            Console.WriteLine("\n loaded element:" + loadUser.UserName);
                        }
                        if (combinedUserList != null)
                        {
                            Console.WriteLine("\nnot first run:" );
                            foreach (var cc in combinedUserList)
                            {
                                Console.WriteLine("\n before union:" + cc.UserName);
                            }


                            IEnumerable<UserLocation> tmp = loadedUserList.AsEnumerable();
                            combinedUserList = tmp.Union<UserLocation>(combinedUserList.AsEnumerable(), new UserComparer2()).ToList();

                            Console.WriteLine("\nnot first run after union:" );
                            foreach (var cc in combinedUserList)
                            {
                                Console.WriteLine("\n after union the user name is:" + cc.UserName);
                            }

                        }
                        else
                        {
                            combinedUserList = loadedUserList;
                            Console.WriteLine("\nfirst run the count is:" + combinedUserList.Count());
                            foreach (var cc in combinedUserList)
                            {
                                Console.WriteLine("\n the combined list:" + cc.UserName);
                            }
                        }

                        var maxID = loadedUserList.Max(myMaxID => myMaxID.ID);

                        lastedLoadedRecordID = lastedLoadedRecordID + 1;

                    }
                    else
                    {
                        Console.WriteLine("\n no new data!");
                        Console.WriteLine("\n-----------------load end,no new data yet------------------------------------------------");

                        Thread.Sleep(3000);
                    }
                    loadStopwatch.Stop();
                    Console.WriteLine("\nload time cost{0} seconds。", loadStopwatch.Elapsed);
                    Console.WriteLine("\n---------------------load end ----------------------------------------------------------");

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n exception message:" + ex.Message);

            }




        }

    }


    class UserComparer2 : IEqualityComparer<UserLocation>
    {
        public bool Equals(UserLocation x, UserLocation y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.ID == y.ID && x.UserName == y.UserName;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(UserLocation user)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(user, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashUserName = user.UserName == null ? 0 : user.UserName.GetHashCode();

            //Get hash code for the Code field.
            int hashUserCode = user.ID.GetHashCode();

            //Calculate the hash code for the product.
            return hashUserName ^ hashUserCode;
        }

    }
}
Program.cs
静态void Main(字符串[]参数)
{
对于(int i=1;i<3;i++)
{
WriteLine(“\n运行{0}次,i);
Test.LoadUsersFromDB();
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Data.Spatial;
使用System.Data.Entity;
使用System.Xml.Linq;
使用System.IO;
使用系统配置;
使用System.Web;
使用System.Web.Script.Serialization;
使用系统集合;
利用制度全球化;
命名空间SptialMatch
{
课堂测试
{
公共静态int lastedLoadedRecordID=14;
公共静态IEnumerable loadedUserList;
公共静态IEnumerable组合服务列表;
公共静态void LoadUsersFromDB()
{
尝试
{
Console.WriteLine(“\n------------------load data begin--------------------------------------------------------------”;
//var context=new SptialTestEntities();
使用(var context=new sptialestantities())
{
System.Diagnostics.Stopwatch loadStopwatch=新的System.Diagnostics.Stopwatch();
loadStopwatch.Start();
loadedUserList=来自context.UserLocations中的newRcords
其中newRcords.ID>lastedLoadedRecordID
选择新单词;
if(loadedUserList.Any())
{
foreach(loadedUserList中的var loadUser)
{
Console.WriteLine(“\n加载的元素:“+loadUser.UserName”);
}
if(combinedUserList!=null)
{
Console.WriteLine(\n不是第一次运行:);
foreach(组合服务器列表中的var cc)
{
Console.WriteLine(“\n联合前:”+cc.UserName);
}
IEnumerable tmp=loadedUserList.AsEnumerable();
combinedUserList=tmp.Union(combinedUserList.AsEnumerable(),new UserComparer2()).ToList();
Console.WriteLine(“\n在union之后不能第一次运行:”);
foreach(组合服务器列表中的var cc)
{
Console.WriteLine(“\n联合后的用户名为:”+cc.UserName);
}
}
其他的
{
combinedUserList=loadedUserList;
Console.WriteLine(“\n第一次运行计数为:”+combinedUserList.count());
foreach(组合服务器列表中的var cc)
{
Console.WriteLine(\n组合列表:“+cc.UserName”);
}
}
var maxID=loadedUserList.Max(myMaxID=>myMaxID.ID);
lastedLoadedRecordID=lastedLoadedRecordID+1;
}
其他的
{
Console.WriteLine(“\n无新数据!”);
Console.WriteLine(“\n--------------------加载结束,还没有新数据---------------------------------------------------”);
睡眠(3000);
}