當前位置: 首頁>>代碼示例>>C#>>正文


C# Columns.Select方法代碼示例

本文整理匯總了C#中Columns.Select方法的典型用法代碼示例。如果您正苦於以下問題:C# Columns.Select方法的具體用法?C# Columns.Select怎麽用?C# Columns.Select使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Columns的用法示例。


在下文中一共展示了Columns.Select方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的Columns.Select方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。