SqlRemoveConstantOrderBy.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / SqlClient / Query / SqlRemoveConstantOrderBy.cs / 1305376 / SqlRemoveConstantOrderBy.cs

                            using System; 
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Data; 

namespace System.Data.Linq.SqlClient { 
    using System.Data.Linq; 

    ///  
    /// SQL doesn't allow constants in ORDER BY.
    ///
    /// Worse, an integer constant greater than 0 is treated as ORDER BY ProjectionColumn[i] so the results
    /// can be unexpected. 
    ///
    /// The LINQ semantic for OrderBy(o=>constant) is for it to have no effect on the ordering. We enforce 
    /// that semantic here by removing all constant columns from OrderBy. 
    /// 
    internal class SqlRemoveConstantOrderBy { 

        private class Visitor : SqlVisitor {
            internal override SqlSelect VisitSelect(SqlSelect select) {
                int i = 0; 
                List orders = select.OrderBy;
                while (i < orders.Count) { 
                    SqlExpression expr = orders[i].Expression; 
                    while (expr.NodeType == SqlNodeType.DiscriminatedType) {
                        expr = ((SqlDiscriminatedType)expr).Discriminator; 
                    }
                    switch (expr.NodeType) {
                        case SqlNodeType.Value:
                        case SqlNodeType.Parameter: 
                            orders.RemoveAt(i);
                            break; 
                        default: 
                            ++i;
                            break; 
                    }
                }
                return base.VisitSelect(select);
            } 
        }
 
        ///  
        /// Remove relative constants from OrderBy.
        ///  
        internal static SqlNode Remove(SqlNode node) {
            return new Visitor().Visit(node);
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK