linq - how to combine query results of several times function call with entity framework5? -


i want use function query db linq , combine results,i write code follows cannot work , 1 can me? thanks!

the error:(the operation cannot completed because dbcontext has been disposed)

the part code:

public static ienumerable<userlocation> loadeduserlist;  public static ienumerable<userlocation> combineduserlist;   public static void loaddatainstaticclass()             {                 using (var context = new sptialtestentities())                 {                     var loadeduserlist = newrcords in context.userlocations                                          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);                             }                          }                     }                  }               } 

the problem is: first call ok, second report error: operation cannot completed because dbcontext has been disposed ,and how? thanks!

i paste whole code , 1 can run , check mistake , thank u: userlocation table contain userid,username,userlocation(geography type) ,and user database first mode in visual studio 2012 , map userlocation entity of sptialtestentities.

program.cs static void main(string[] args) {    (int = 1; < 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 = newrcords in context.userlocations                                       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 user name is:" + cc.username);                             }                          }                         else                         {                             combineduserlist = loadeduserlist;                             console.writeline("\nfirst run count is:" + combineduserlist.count());                             foreach (var cc in combineduserlist)                             {                                 console.writeline("\n 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 compared objects reference same data.             if (object.referenceequals(x, y)) return true;              //check whether of compared objects null.             if (object.referenceequals(x, null) || object.referenceequals(y, null))                 return false;              //check whether products' properties equal.             return x.id == y.id && x.username == y.username;         }          // if equals() returns true pair of objects          // gethashcode() must return same value these objects.          public int gethashcode(userlocation user)         {             //check whether object null             if (object.referenceequals(user, null)) return 0;              //get hash code name field if not null.             int hashusername = user.username == null ? 0 : user.username.gethashcode();              //get hash code code field.             int hashusercode = user.id.gethashcode();              //calculate hash code product.             return hashusername ^ hashusercode;         }      } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -