当前位置: 首页>>代码示例>>C#>>正文


C# Columns.Where方法代码示例

本文整理汇总了C#中Columns.Where方法的典型用法代码示例。如果您正苦于以下问题:C# Columns.Where方法的具体用法?C# Columns.Where怎么用?C# Columns.Where使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Columns的用法示例。


在下文中一共展示了Columns.Where方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigurePagedListCommand

        /// <summary>
        /// 根据查询条件分页返回数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecords">总记录数</param>
        /// <param name="criteria"></param>
        /// <param name="columns">数据库信息返回列表</param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigurePagedListCommand(DbCommand command, string tableName, Columns allColumns, int pageIndex, int pageSize, out int totalRecords, Criteria criteria = null, string[] columns = null)
        {
            int index;
            var returnColumns = columns ?? allColumns.Select(c => c.Name);
            var commandTextBuilder = new StringBuilder();
            var totalRecordCommandTextBuilder = new StringBuilder();
            var conditionTextBuilder = new StringBuilder();

            commandTextBuilder.Append("SELECT ");
            totalRecordCommandTextBuilder.AppendFormat("SELECT COUNT(*) FROM {0} ", this.GetSafeTableName(tableName));

            commandTextBuilder.Append("ROW_NUMBER() OVER(ORDER BY ");

            var primaryKeys = allColumns.Where(c => c.IsPrimaryKey);

            // 添加排序条件。
            if (criteria != null && !criteria.Orders.IsEmpty())
            {
                index = 1;
                foreach (var order in criteria.Orders)
                {
                    commandTextBuilder.AppendFormat((criteria.Orders.Count == index++) ? "\"{0}\" {1}" : "\"{0}\" {1},",
                        allColumns[order.PropertyName].Name, order.OrderBy);
                }
            }
            else if (!primaryKeys.IsEmpty())
            {
                index = 1;
                foreach (var item in primaryKeys)
                {
                    commandTextBuilder.AppendFormat(primaryKeys.Count() == index ? "\"{0}\"" : "\"{0}\",", item.Name);

                    index++;
                }
            }
            else
            {
                commandTextBuilder.AppendFormat("\"{0}\"", allColumns.FirstOrDefault().Name);
            }

            commandTextBuilder.Append(") AS \"RowIndex\"");

            foreach (var item in returnColumns)
            {
                commandTextBuilder.AppendFormat(",\"{0}\"", allColumns[item].Name);
            }

            commandTextBuilder.AppendFormat(" FROM {0} ", this.GetSafeTableName(tableName));

            var effectiveConditions = criteria?.EffectiveConditions;

            // 添加查询条件。
            if (!effectiveConditions.IsEmpty())
            {
                index = 1;
                foreach (var item in effectiveConditions)
                {
                    var fragment = this.CreateFragment(allColumns[item.PropertyName].Name, item.Op);

                    conditionTextBuilder.Append(fragment.Item1);

                    if (index < effectiveConditions.Count)
                    {
                        conditionTextBuilder.AppendFormat(" {0} ", effectiveConditions[index].JoinType);
                    }

                    if ((int)item.Op <= 5)
                    {
                        command.AddParameter($":{fragment.Item2}", item.Value);
                    }

                    index++;
                }
            }

            if (!string.IsNullOrWhiteSpace(conditionTextBuilder.ToString()))
            {
                commandTextBuilder.Append("WHERE ");
                commandTextBuilder.Append(conditionTextBuilder);

                totalRecordCommandTextBuilder.Append("WHERE ");
                totalRecordCommandTextBuilder.Append(conditionTextBuilder);
            }

            commandTextBuilder.Append(" Order By \"RowIndex\" ASC ");
            commandTextBuilder.AppendFormat("OFFSET (:pageIndex-1)*:pageSize LIMIT :pageSize");

            command.CommandType = CommandType.Text;
//.........这里部分代码省略.........
开发者ID:fenglinz,项目名称:Sparrow,代码行数:101,代码来源:PostgreSQLDynamicQueryProvider.cs

示例2: ConfigureCreateOrUpdateCommand

        /// <summary>
        /// 添加或者更新数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigureCreateOrUpdateCommand(DbCommand command, string tableName, Columns allColumns)
        {
            var primaryKeys = allColumns.Where(c => c.IsPrimaryKey);
            var updateColumns = allColumns.Where(c => !c.IsPrimaryKey && !c.IsIdentity);
            var insertColumns = allColumns.Where(c => !c.IsIdentity);
            var commandTextBuilder = new StringBuilder();

            if (primaryKeys.IsEmpty())
            {
                throw new Exception("实体没有设置主键!");
            }

            commandTextBuilder.Append("BEGIN;");
            commandTextBuilder.AppendFormat("UPDATE {0} SET ", this.GetSafeTableName(tableName));

            var index = 1;
            foreach (var item in updateColumns)
            {
                var format = index++ == updateColumns.Count() ? "\"{0}\"=:{0}1" : "\"{0}\"=:{0}1,";

                commandTextBuilder.AppendFormat(format, item.Name);
                command.AddParameter($":{item.Name}1", item.Value, this._dbTypeResolve.GetDbType(item.DataType));
            }

            commandTextBuilder.Append(" WHERE ");

            index = 1;
            foreach (var item in primaryKeys)
            {
                commandTextBuilder.AppendFormat(primaryKeys.Count() == index++ ? "\"{0}\"=:{0}1" : "\"{0}\"=:{0}1 AND ", item.Name);
                command.AddParameter($":{item.Name}1", item.Value, this._dbTypeResolve.GetDbType(item.DataType));
            }

            commandTextBuilder.Append(";");
            commandTextBuilder.AppendFormat("INSERT INTO {0} (", this.GetSafeTableName(tableName));

            index = 1;
            foreach (var column in insertColumns)
            {
                commandTextBuilder.AppendFormat(index++ == insertColumns.Count() ? "\"{0}\"" : "\"{0}\",", column.Name);
            }

            commandTextBuilder.AppendFormat(") SELECT ");

            index = 1;
            foreach (var column in insertColumns)
            {
                var format = index++ == insertColumns.Count() ? ":{0}2" : ":{0}2,";

                commandTextBuilder.AppendFormat(format, column.Name);
                command.AddParameter($":{column.Name}2", column.Value, this._dbTypeResolve.GetDbType(column.DataType));
            }

            commandTextBuilder.AppendFormat(" WHERE NOT EXISTS(SELECT 1 FROM {0} WHERE ", this.GetSafeTableName(tableName));

            index = 1;
            foreach (var primaryKey in primaryKeys)
            {
                commandTextBuilder.AppendFormat(primaryKeys.Count() == (index++) ? "\"{0}\"=:{0}" : "\"{0}\"=:{0} AND ", primaryKey.Name);
                command.AddParameter($":{primaryKey.Name}", primaryKey.Value, this._dbTypeResolve.GetDbType(primaryKey.DataType));
            }

            commandTextBuilder.Append("); ");
            commandTextBuilder.Append("COMMIT;");

            command.CommandType = CommandType.Text;
            command.CommandText = commandTextBuilder.ToString();

            this.RecordCommandInfo("添加或者更新数据", command);
        }
开发者ID:fenglinz,项目名称:Sparrow,代码行数:77,代码来源:PostgreSQLDynamicQueryProvider.cs

示例3: ConfigureUpdateCommand

        /// <summary>
        /// 更新数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <param name="columns">更新列集合</param>
        /// <param name="criteria"></param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigureUpdateCommand(DbCommand command, string tableName, Columns allColumns, Columns columns, Criteria criteria = null)
        {
            var index = 1;
            var commandTextBuilder = new StringBuilder();

            commandTextBuilder.AppendFormat("UPDATE {0} SET ", this.GetSafeTableName(tableName));

            foreach (var item in columns)
            {
                var format = index++ == columns.Count() ? "\"{0}\"=:{0}" : "\"{0}\"=:{0},";

                commandTextBuilder.AppendFormat(format, columns[item.Name].Name);
                command.AddParameter($":{columns[item.Name].Name}", item.Value, this._dbTypeResolve.GetDbType(columns[item.Name].DataType));
            }

            var effectiveConditions = criteria?.EffectiveConditions;

            if (effectiveConditions.IsEmpty())
            {
                var primaryKeys = columns.Where(c => c.IsPrimaryKey).Select(c => c.Name);

                if (primaryKeys.Any())
                {
                    commandTextBuilder.Append(" WHERE ");

                    index = 1;
                    foreach (var item in primaryKeys)
                    {
                        commandTextBuilder.AppendFormat(primaryKeys.Count() == index++ ? " \"{0}\"=:{0}" : "\"{0}\"=:{0} AND ", columns[item].Name);
                        command.AddParameter($":{columns[item].Name}", columns[item].Value, this._dbTypeResolve.GetDbType(columns[item].DataType));
                    }
                }
            }
            else
            {
                index = 1;
                commandTextBuilder.Append(" WHERE ");

                foreach (var item in effectiveConditions)
                {

                    var columnName = allColumns[item.PropertyName].Name;
                    var fragment = this.CreateFragment(columnName, item.Op);

                    commandTextBuilder.Append(fragment.Item1);

                    if (index < effectiveConditions.Count)
                    {
                        commandTextBuilder.AppendFormat(" {0} ", effectiveConditions[index].JoinType);
                    }

                    if ((int)item.Op <= 5)
                    {
                        command.AddParameter($":{fragment.Item2}", item.Value, this._dbTypeResolve.GetDbType(columns[item.PropertyName].DataType));
                    }

                    index++;
                }
            }

            command.CommandType = CommandType.Text;
            command.CommandText = commandTextBuilder.ToString();

            this.RecordCommandInfo("更新记录", command);
        }
开发者ID:fenglinz,项目名称:Sparrow,代码行数:74,代码来源:PostgreSQLDynamicQueryProvider.cs

示例4: ConfigureCreateCommand

        /// <summary>
        /// 添加数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigureCreateCommand(DbCommand command, string tableName, Columns allColumns)
        {
            var index = 1;
            var insertColumns = allColumns.Where(c => !c.IsIdentity);
            var commandTextBuilder = new StringBuilder();

            commandTextBuilder.AppendFormat("INSERT INTO {0} (", this.GetSafeTableName(tableName));

            foreach (var column in insertColumns)
            {
                commandTextBuilder.AppendFormat(index++ == insertColumns.Count() ? "\"{0}\"" : "\"{0}\",", column.Name);
            }

            commandTextBuilder.AppendFormat(") VALUES (");

            index = 1;
            foreach (var item in insertColumns)
            {
                var format = index++ == insertColumns.Count() ? ":{0}" : ":{0}, ";

                commandTextBuilder.AppendFormat(format, item.Name);
                command.AddParameter($":{item.Name}", item.Value, this._dbTypeResolve.GetDbType(item.DataType));
            }

            commandTextBuilder.Append(")");

            if (allColumns.Any(c => c.IsIdentity))
            {
                var identity = allColumns.First(i => i.IsIdentity);

                commandTextBuilder.AppendFormat(";SELECT currval(pg_get_serial_sequence('{0}', '{1}'));", this.GetSafeTableName(tableName), identity.Name);
            }

            command.CommandType = CommandType.Text;
            command.CommandText = commandTextBuilder.ToString();

            this.RecordCommandInfo("添加记录", command);
        }
开发者ID:fenglinz,项目名称:Sparrow,代码行数:45,代码来源:PostgreSQLDynamicQueryProvider.cs

示例5: ConfigureCreateCommand

        /// <summary>
        /// 添加数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigureCreateCommand(DbCommand command, string tableName, Columns allColumns)
        {
            var index = 1;
            var insertColumns = allColumns.Where(c => !c.IsIdentity);
            var commandTextBuilder = new StringBuilder();

            commandTextBuilder.AppendFormat("INSERT INTO {0} (", this.GetSafeTableName(tableName));

            foreach (var column in insertColumns)
            {
                commandTextBuilder.AppendFormat(index++ == insertColumns.Count() ? "[{0}]" : "[{0}],", column.Name);
            }

            commandTextBuilder.AppendFormat(") VALUES (");

            index = 1;
            foreach (var item in insertColumns)
            {
                var format = index++ == insertColumns.Count() ? "@{0}" : "@{0}, ";

                commandTextBuilder.AppendFormat(format, item.Name);
                command.AddParameter($"@{item.Name}", item.Value);
            }

            commandTextBuilder.Append(")");

            if (allColumns.Any(c => c.IsIdentity))
            {
                commandTextBuilder.Append(";SELECT @@IDENTITY;");
            }

            command.CommandType = CommandType.Text;
            command.CommandText = commandTextBuilder.ToString();

            this.RecordCommandInfo("添加记录", command);
        }
开发者ID:fenglinz,项目名称:Sparrow,代码行数:43,代码来源:MSSQLDynamicQueryProvider.cs

示例6: ConfigureCreateOrUpdateCommand

        /// <summary>
        /// 添加或者更新数据。
        /// </summary>
        /// <param name="command">SQL命令执行对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="allColumns">所有列信息</param>
        /// <returns>命令执行对象</returns>
        protected override void ConfigureCreateOrUpdateCommand(DbCommand command, string tableName, Columns allColumns)
        {
            var index = 1;
            var primaryKeys = allColumns.Where(c => c.IsPrimaryKey);
            var updateColumns = allColumns.Where(c => !c.IsPrimaryKey && !c.IsIdentity);
            var insertColumns = allColumns.Where(c => !c.IsIdentity);
            var commandTextBuilder = new StringBuilder();

            if (primaryKeys.IsEmpty())
            {
                throw new Exception("实体没有设置主键!");
            }

            commandTextBuilder.AppendFormat("IF EXISTS(SELECT * FROM {0} WHERE ", this.GetSafeTableName(tableName));

            foreach (var primaryKey in primaryKeys)
            {
                commandTextBuilder.AppendFormat(primaryKeys.Count() == (index++) ? "[{0}][email protected]{0}" : " [{0}][email protected]{0} AND", primaryKey.Name);
                command.AddParameter($"@{primaryKey.Name}", primaryKey.Value);
            }

            commandTextBuilder.Append(") ");

            commandTextBuilder.AppendFormat("UPDATE {0} SET ", this.GetSafeTableName(tableName));

            index = 1;
            foreach (var item in updateColumns)
            {
                var format = index++ == updateColumns.Count() ? "[{0}][email protected]{0}1" : "[{0}][email protected]{0}1,";

                commandTextBuilder.AppendFormat(format, item.Name);
                command.AddParameter($"@{item.Name}1", item.Value);
            }

            commandTextBuilder.Append(" WHERE ");

            index = 1;
            foreach (var item in primaryKeys)
            {
                commandTextBuilder.AppendFormat(primaryKeys.Count() == index++ ? "[{0}][email protected]{0}1" : "[{0}][email protected]{0}1 AND ", item.Name);
                command.AddParameter($"@{item.Name}1", item.Value);
            }

            commandTextBuilder.Append(" ELSE ");
            commandTextBuilder.AppendFormat("INSERT INTO {0} (", this.GetSafeTableName(tableName));

            index = 1;
            foreach (var column in insertColumns)
            {
                commandTextBuilder.AppendFormat(index++ == insertColumns.Count() ? "[{0}]" : "[{0}],", column.Name);
            }

            commandTextBuilder.AppendFormat(") VALUES (");

            index = 1;
            foreach (var column in insertColumns)
            {
                var format = index++ == insertColumns.Count() ? "@{0}2" : "@{0}2,";

                commandTextBuilder.AppendFormat(format, column.Name);
                command.AddParameter($"@{column.Name}2", column.Value);
            }

            commandTextBuilder.Append(")");

            command.CommandType = CommandType.Text;
            command.CommandText = commandTextBuilder.ToString();

            this.RecordCommandInfo("添加或者更新数据", command);
        }
开发者ID:fenglinz,项目名称:Sparrow,代码行数:77,代码来源:MSSQLDynamicQueryProvider.cs


注:本文中的Columns.Where方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。