Last active
June 13, 2022 19:28
-
-
Save SoulFireMage/3d3008961ba892548856fea3c99c3615 to your computer and use it in GitHub Desktop.
Chunk data into 2k chunks and update in Sql.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''This code just saved 6700 mergecodes across to the db in a few second compared to long minutes via EF :) | |
Public Shared Sub SaveMany(Items As List(Of BranchContact)) | |
If Items Is Nothing Then Return | |
Using db As New LogIQEntities | |
Dim chunkedItems = ChunkData(Items, 500) 'I've not tested to see the upper/lower bounds of benefits/deficits on this figure :P | |
For Each chunkedList In chunkedItems | |
Dim Query = New StringBuilder | |
For Each i In chunkedList | |
Query.Append($"update BranchContacts set Mergecodes = '{i.Mergecodes}' where Emailid = {i.Emailid} and Branch = '{i.Branch}';") | |
Next | |
db.Database.ExecuteSqlCommand(Query.ToString) | |
Next | |
' db.SaveChanges() | |
End Using | |
End Sub | |
Public Shared Iterator Function ChunkData(Of T)(source As IEnumerable(Of T), chunkSize As Integer) As IEnumerable(Of IEnumerable(Of T)) | |
Dim _chunkSize = If(source.Count < chunkSize, source.Count, chunkSize) | |
Dim i = 0 | |
While i < source.Count() | |
Yield source.Skip(i).Take(_chunkSize) | |
i += _chunkSize | |
End While | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment