Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One potential issue with your technique is that it doesn't handle very large lists - you may exceed the maximum string length for your database. I use a helper method that concatenates the integer values into an enumeration of strings, each of which is less than a specified maximum (the following implementation also optionally checks for and removes duplicates ids):</p> <pre><code>public static IEnumerable&lt;string&gt; ConcatenateValues(IEnumerable&lt;int&gt; values, string separator, int maxLength, bool skipDuplicates) { IDictionary&lt;int, string&gt; valueDictionary = null; StringBuilder sb = new StringBuilder(); if (skipDuplicates) { valueDictionary = new Dictionary&lt;int, string&gt;(); } foreach (int value in values) { if (skipDuplicates) { if (valueDictionary.ContainsKey(value)) continue; valueDictionary.Add(value, ""); } string s = value.ToString(CultureInfo.InvariantCulture); if ((sb.Length + separator.Length + s.Length) &gt; maxLength) { // Max length reached, yield the result and start again if (sb.Length &gt; 0) yield return sb.ToString(); sb.Length = 0; } if (sb.Length &gt; 0) sb.Append(separator); sb.Append(s); } // Yield whatever's left over if (sb.Length &gt; 0) yield return sb.ToString(); } </code></pre> <p>Then you use it something like:</p> <pre><code>using(SqlCommand command = ...) { command.Connection = ...; command.Transaction = ...; // if in a transaction SqlParameter parameter = command.Parameters.Add("@Items", ...); foreach(string itemList in ConcatenateValues(values, "~", 8000, false)) { parameter.Value = itemList; command.ExecuteNonQuery(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload