How to prevent memory overflow when using an IEnumerable and Linq-To-Sql?

后端 未结 4 730
旧时难觅i
旧时难觅i 2021-01-16 02:53

This question is related to a previous question of mine

That\'s my current code

 IEnumerable Get()
 {
     while(//get implementation
           


        
4条回答
  •  春和景丽
    2021-01-16 03:26

    Use the following extension method to break the input into appropriately sized subsets

    public static class IEnumerableExtensions
    {
        public static IEnumerable> InSetsOf(this IEnumerable source, int max)
        {
            List toReturn = new List();
            foreach(var item in source)
            {
                toReturn.Add(item);
                if (toReturn.Count == max)
                {
                    yield return toReturn;
                    toReturn = new List();
                }
            }
            if (toReturn.Any())
            {
                yield return toReturn;
            }
        }
    }
    

    then persist the subsets

    void Insert()
    {
        var actual = Get();
        using (var db = new DataClassesDataContext())
        {
            foreach (var set in actual.InSetsOf(5))
            {
                db.Shapes.InsertAllOnSubmit(set);
                db.SubmitChanges();
            }
        }
    }
    

    You might also find this MSDN article on InsertOnSubmit() vs InsertAllOnSubmit() to be useful.

提交回复
热议问题