We encountered this problem at work. The problem is that list.Contains() creates a WHERE column IN (val1, val2, ... valN) statement WHERE column IN (val1, val2, ... valN) , so you are limited by how many values ββyou can put there. What we finished actually did it in the games just like you did.
However, I think I can offer you a cleaner and more elegant code to do this. The following is an extension method that will be added to other Linq methods that you usually use:
public static IEnumerable<IEnumerable<T>> BulkForEach<T>(this IEnumerable<T> list, int size = 1000) { for (int index = 0; index < list.Count() / size + 1; index++) { IEnumerable<T> returnVal = list.Skip(index * size).Take(size).ToList(); yield return returnVal; } }
Then you use it as follows:
foreach (var item in list.BulkForEach()) {
EDIT
Or, if you want, you can make it act like a normal List.ForEach () as follows:
public static void BulkForEach<T>(this IEnumerable<T> list, Action<IEnumerable<T>> action, int size = 1000) { for (int index = 0; index < list.Count() / size + 1; index++) { IEnumerable<T> returnVal = list.Skip(index * size).Take(size).ToList(); action.Invoke(returnVal); } }
Used as follows:
list.BulkForEach(p => { });
source share