Convert an array of integers for use in a SQL “IN” clause

前端 未结 8 2177
死守一世寂寞
死守一世寂寞 2021-01-05 03:44

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL \"IN\" clause?

e.g.

8条回答
  •  北海茫月
    2021-01-05 04:24

    Hey, great suggestions, just a slight modification below

    public static class IEnumerableExtensions
    {
        // reasonable to assume you will use this everywhere, not just
        // Sql statements, but log statements, anywhere you need to 
        // dump a list into a readable format!
        // 
        // HINT: extra credit: you can generalize this, and provide
        // specialized short hands that invoke the general method
        public static string ToCommaSeparatedString(this IEnumerable values)
        {
             // SIGH: so apparently this does not generate minimal
             // assembler on every machine, please note the following
             // is written for clarity, please feel free to substitute
             // your own favourite ultra-performance high-octance
             // string appender algorithm
             StringBuilder commaSeparated = new StringBuilder ();
             foreach (T value in values)
             {
                 // PERF: store format string as const
                 commaSeparated.AppendFormat ("{0}, ", value);
             }
             // PERF: store trim chars as static readonly array
             return commaSeparated.Trim (", ".ToCharArray ());
        }
    }
    
    ...
    // elsewhere in code
    List myIdentifiers = new List { 1, 2, 3, 4, 5, };
    string mySqlIdentifierList = myIdentifiers.ToCommaSeparatedList ();
    string mySqlStatementFormat = "SELECT * FROM [SomeTable] WHERE [Id] IN ({0})";
    string mySqlStatement = 
        string.format (mySqlStatementFormat, mySqlIdentifierList);
    ...
    

提交回复
热议问题