T>如何使用一个IEnumerable&LT时prevent内存溢出;和LINQ到SQL?如何使用、内存、IEnumerable、GT

由网友(悲催的曾经)分享简介:这个问题是关系到我的previous问题 这就是我目前的code That's my current codeIEnumerable Get(){while(//get implementationyield return new Shape(//...}void Insert(){var actua...

这个问题是关系到我的previous问题

这就是我目前的code

That's my current code

 IEnumerable<Shape> Get()
 {
     while(//get implementation
         yield return new Shape(//...
 }

 void Insert()
 {
       var actual = Get();
       using (var db = new DataClassesDataContext())
       {
           db.Shapes.InsertAllOnSubmit(actual);
           db.SubmitChanges();
       }
 }

我收到一个内存溢出,因为IEnumerable的太大。我如何prevent呢?

I'm getting a memory overflow, since the IEnumerable is too big. How do I prevent it?

推荐答案

尝试使用的 InsertOnSubmit 而不是 InsertAllOnSubmit 。然后提交在适当的间隔,如埃里希说。

Try using InsertOnSubmit rather than InsertAllOnSubmit. And then commit at appropriate intervals, like Erich said.

或者,如果你想做到这一点的例如批次5,尽量Handcraftsman's或获取的IEnumerable的的IEnumerable的DTB 的解决方案。例如,与DTB的块:

Or, if you want to do it in batches of e.g. 5, try Handcraftsman's or dtb's solutions for getting IEnumerable's of IEnumerable. E.g., with dtb's Chunk:

   var actual = Get();
   using (var db = new DataClassesDataContext())
   {
       foreach(var batch in actual.Chunk(5))
       {
         db.Shapes.InsertAllOnSubmit(batch);
         db.SubmitChanges();
       }
   }
阅读全文

相关推荐

最新文章