Beispiel #1
0
        /// <summary>
        ///     Adds "<paramref name="columnName" /> not in (<paramref name="parameters" />.ParameterName, ...)"
        ///     expression to the clause.
        /// </summary>
        public static SqlClause AddNotIn(this SqlClause sqlClause,
                                         [NotNull] string columnName,
                                         [CanBeNull] IEnumerable <IDbDataParameter> parameters)
        {
            if (parameters == null)
            {
                return(sqlClause);
            }

            var list = parameters as IReadOnlyList <IDbDataParameter>;

            if (list == null)
            {
                list = parameters.ToList();
            }

            if (list.Count == 0)
            {
                return(sqlClause);
            }

            if (list.Count == 1)
            {
                return(sqlClause.AddNotEquals(columnName, list[0]));
            }


            var expression = new StringBuilder(columnName);

            expression.Append(" not in (");

            for (var k = 0; k < list.Count; k++)
            {
                var p = list[k];

                if (k > 0)
                {
                    expression.Append(", ");
                }

                expression.Append(p.ParameterName);
                sqlClause.Add(p);
            }

            expression.Append(')');

            sqlClause.Add(expression.ToString());

            return(sqlClause);
        }