Pass subsets of datatable to SQL Server
I am trying to figure out how I could breakup my datatable and send that (as UDT) down to sql server). So if I have 100,000 in my datatable I would like to be able to break it up into 10,000 chunks to send down to my sql server. I am just not sur开发者_开发百科e about how I would take the datatable with 100,000 and break it up into those 10,000k blocks?
Any suggestions would be greatly appreciated.
So if I used something like this:
var results = (from myRow in myDataTable.AsEnumerable()
select myRow).take(10000);
Not sure how I could ensure I grabbed the next set of 10,000 rows from the datatable and ensure I don't send duplicates and get all of the rows?
Just write yourself a simple method to turn any collection into chunks of a specified size:
/// <summary>Splits a collection into chunks of equal size. The last chunk may be smaller than chunkSize, but all chunks, if any, will contain at least one element.</summary>
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize)
{
if (chunkSize <= 0)
throw new ArgumentException("chunkSize must be greater than zero.", "chunkSize");
return chunkIterator(source, chunkSize);
}
private static IEnumerable<IEnumerable<T>> chunkIterator<T>(IEnumerable<T> source, int chunkSize)
{
var list = new List<T>();
foreach (var elem in source)
{
list.Add(elem);
if (list.Count == chunkSize)
{
yield return list;
list = new List<T>();
}
}
if (list.Count > 0)
yield return list;
}
And then you can trivially use this, for example:
foreach (var chunk in myDataTable.AsEnumerable().Chunk(10000))
{
// Process the chunk
}
精彩评论