Context is always important, such as the size and complexity of the array. For small to mid-size lists, several of the answers posted here are just fine, though some clarifications should be made:
text()
XML function. For more info (i.e. performance analysis) on using XML to split lists, check out "Using XML to pass lists as parameters in SQL Server" by Phil Factor.DataTable
means duplicating the data in memory as it is copied from the original collection. Hence using the DataTable
method of passing in TVPs does not work well for larger sets of data (i.e. does not scale well).DataTable
TVP method, XML does not scale well as it more than doubles the datasize in memory as it needs to additionally account for the overhead of the XML document.With all of that said, IF the data you are using is large or is not very large yet but consistently growing, then the IEnumerable
TVP method is the best choice as it streams the data to SQL Server (like the DataTable
method), BUT doesn't require any duplication of the collection in memory (unlike any of the other methods). I posted an example of the SQL and C# code in this answer: