csharp 示例sql搜索查询连接表来编译搜索结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 示例sql搜索查询连接表来编译搜索结果相关的知识,希望对你有一定的参考价值。

SELECT * FROM ( SELECT DISTINCT q.Id, q.ProjectName, q.QuoteOrderStatusId, qs.Name as 'QuoteOrderStatusName', q.UtcIssueDate , q.UtcCreateDate, q.CustomerServiceRepId, um.FirstName as 'CSRFirstName', um.LastName as 'CSRLastName', q.SalesRepId, q.SoldToAddressId, q.TotalPrice,
            q.QuoteNumber, qc.CustomerNumber, qc.CustomerName, at.*, ct.*, ad.*,
        ROW_NUMBER() OVER ( ORDER BY q.quoteNumber Desc ) as RowNumber
		FROM QuoteOrder q
        LEFT JOIN QuoteOrderCustomer qc ON qc.Id = q.QuoteOrderCustomerId
        LEFT JOIN AccountType at ON at.AccountTypeID = qc.AccountTypeID
        LEFT JOIN CustomerType ct ON ct.CustomerTypeID = qc.CustomerTypeID
        LEFT JOIN Address ad ON ad.AddressID = q.SoldToAddressId
        LEFT JOIN UserMaster um ON um.UserID = q.CustomerServiceRepId
        LEFT JOIN QuoteOrderStatus qs ON qs.Id = q.QuoteOrderStatusId
			WHERE QuoteOrderClassId = 'Q'  AND q.SalesRepId = 2 ) as Quotes1  WHERE RowNumber > 0 and RowNumber <= 30 

        public IPagedListResult<QuoteOrderSummaryEntity> FindMatchingQuotes( QuoteOrderSearchParams args, Page page )
        {
            var parameters = new List<SqlParameter>( );
            var sbSql = new StringBuilder( );

            var where = BuildMachingQuoteWhereClause( args, ref parameters );
            var initialWhereClause = " WHERE QuoteOrderClassId = 'Q' ";

            var totalSql = $" select count( q.Id ) from QuoteOrder q left join QuoteOrderCustomer qc ON qc.Id = q.QuoteOrderCustomerId {initialWhereClause} {where} ";
            var totalParams = parameters.Select( p => new SqlParameter( p.ParameterName, p.Value ) );
            var total = ExecuteScalar<long>( totalSql, totalParams.ToArray( ) );

            // Start with the base SQL. - we're paging, so wrap with ROW_NUMBER select http://www.codeguru.com/csharp/.net/net_data/article.php/c19611/Paging-in-SQL-Server-2005.htm
            sbSql.Append( "SELECT * FROM ( " );

            var sortColumn = SanitizeSortColumn( page.SortField );
            // This join is safe for paging, since there is only a single customer status record per customer
            sbSql.Append( $@"SELECT DISTINCT 
                            q.Id,
                            q.ProjectName,
                            qs.Name as 'QuoteOrderStatusName',
                            q.UtcIssueDate,
                            q.UtcCreateDate,
                            q.CustomerServiceRepId,
                            um.FirstName as 'CSRFirstName',
                            um.LastName as 'CSRLastName',
                            q.SalesRepId,
                            q.SoldToAddressId,
                            q.TotalPrice,
                            q.QuoteNumber,
                            qc.CustomerNumber,
                            qc.CustomerName, at.*, ct.*, ad.*,
                            ROW_NUMBER() OVER ( ORDER BY {sortColumn} {page.Direction} ) as RowNumber
							FROM QuoteOrder q
                            LEFT JOIN QuoteOrderCustomer qc ON qc.Id = q.QuoteOrderCustomerId
                            LEFT JOIN AccountType at ON at.AccountTypeID = qc.AccountTypeID
                            LEFT JOIN CustomerType ct ON ct.CustomerTypeID = qc.CustomerTypeID
                            LEFT JOIN Address ad ON ad.AddressID = q.SoldToAddressId
                            LEFT JOIN UserMaster um ON um.UserID = q.CustomerServiceRepId
                            LEFT JOIN QuoteOrderStatus qs ON qs.Id = q.QuoteOrderStatusId
							" );

            // Add initial where clause.
            sbSql.Append(initialWhereClause);
            sbSql.Append( where );

            // Finish paging wrapper
            sbSql.Append( " ) as Quotes1 " );
            sbSql.Append( " WHERE RowNumber > @PageBegin and RowNumber <= @PageEnd " );
            parameters.Add( new SqlParameter( "PageBegin", page.Skip ) );
            parameters.Add( new SqlParameter( "PageEnd", page.Skip + page.Take ) );

            var pageParams = parameters.Select( p => new SqlParameter( p.ParameterName, p.Value ) );
            var quotes = QueryObjects( sbSql.ToString( ), BuildQuoteOrderSummaryEntity, pageParams.ToArray( ) );

            var result = new PagedListResult<QuoteOrderSummaryEntity>( quotes, total, page );
            return result;
        }

        protected QuoteOrderSummaryEntity BuildQuoteOrderSummaryEntity( SqlDataReader dr )
        {
            var rval = new QuoteOrderSummaryEntity
            {
                Id = dr.GetInt32( "Id" ).Value,
                CustomerName = dr.GetString( "CustomerName" ),
                CustomerNumber = dr.GetString( "CustomerNumber" ),
                ProjectName = dr.GetString( "ProjectName" ),
                CreatedDate = dr.GetDateTime( "utcCreateDate" ),
                QuoteNumber = dr.GetString( "QuoteNumber" ),
                QuoteOrderStatus = new OrderStatus
                {
                    Name = dr.GetString("QuoteOrderStatusName")
                },
            };

            if ( dr.GetByte( "AccountTypeID" ) != null )
            {
                rval.AccountType = new AccountTypeEntity( )
                {
                    AccountTypeID = dr.GetByte( "AccountTypeID" ).Value,
                    AccountTypeDescription = dr.GetString( "AccountTypeDescription" ),
                    IsRequireCustomerNumber = dr.GetBoolean( "IsRequireCustomerNumber" ).Value,
                    Rank = dr.GetByte( "Rank" ).Value
                };
            }

            if( dr.GetString( "CustomerTypeID" ) != null )
            {
                rval.CustomerType = new CustomerTypeEntity
                {
                    CustomerTypeID = dr.GetString( "CustomerTypeID" ),
                    CustomerTypeDescription = dr.GetString( "CustomerTypeDescription" )
                };
            }

            if( dr.GetInt32( "SoldToAddressId" ).HasValue )
            {
                rval.SoldToAddress = new AddressEntity( )
                {
                    AddressID = dr.GetInt32( "AddressID" ).Value,
                    CompanyName = dr.GetString( "CompanyName" ),
                    Address1 = dr.GetString( "Address1" ),
                    Address2 = dr.GetString( "Address2" ),
                    Address3 = dr.GetString( "Address3" ),
                    Address4 = dr.GetString( "Address4" ),
                    City = dr.GetString( "City" ),
                    Region = dr.GetString( "Region" ),
                    PostalCode = dr.GetString( "PostalCode" ),
                    CountryCode = dr.GetString( "CountryCode" ),
                    Phone = dr.GetString( "Phone" ),
                    PhoneExt = dr.GetString( "PhoneExt" ),
                    Fax = dr.GetString( "Fax" ),
                    FaxExt = dr.GetString( "FaxExt" ),
                    Note = dr.GetString( "Note" ),
                    CreateDate = dr.GetDateTime( "CreateDate" ).Value,
                    EditDate =
                        dr.GetDateTime( "EditDate" ).HasValue ? dr.GetDateTime( "EditDate" ).Value : default( DateTime? ),
                    IsShipToAddress = dr.GetBoolean( "IsShipToAddress" ).Value,
                    IsSoldToAddress = dr.GetBoolean( "IsSoldToAddress" ).Value,
                };
            }

            if (dr.GetInt32("CustomerServiceRepId") != null)
            {
                rval.CustomerServiceRep = new UserMaster
                {
                    FirstName = dr.GetString("CSRFirstName"),
                    LastName = dr.GetString("CSRLastName")
                };
            }

            if (dr.GetDateTime("UtcIssueDate") != null)
            {
                rval.QuoteIssuedDate = dr.GetDateTime("UtcIssueDate");
            }

            return rval;
        }

以上是关于csharp 示例sql搜索查询连接表来编译搜索结果的主要内容,如果未能解决你的问题,请参考以下文章

sql MS SQL全文搜索查询示例

SQL Server中的阿拉伯语FullText搜索

SQL Query - 在一个结果行中显示连接结果[重复]

SQL Server 查询以计算每日“持仓”未结订单

csharp 带有突出显示的完整ajax搜索示例

使用多个表和连接加速 SQL 搜索