コード例 #1
0
        /// <summary>Removes duplicates from the tree caused by wildcards and sorts the projected properties.</summary>
        /// <param name="provider">underlying provider instance.</param>
        /// <remarks>
        /// Examples
        /// $select=Orders, Orders/ID           - get rid of the Orders/ID
        /// $select=Orders, Orders/*            - get rid of the Orders/*
        /// $select=Orders/*, Orders/ID         - get rid of the Orders/ID
        /// $select=Orders/*, Orders/OrderItems&amp;$expand=Orders - get rid of the Orders/OrderItems (it's redundant to *)
        /// $select=Orders/*, Orders/OrderItems&amp;$expand=Orders/OrderItems - leave as is, the Orders/OrderItems are expanded
        ///
        /// The sorting order is the same as the order in which the properties are enumerated on the owning type.
        /// This is to preserve the same order as if no projections occured.
        /// </remarks>
        internal void ApplyWildcardsAndSort(DataServiceProviderWrapper provider)
        {
            // If this segment was marked to include entire subtree
            // simply remove all children which are not expanded
            // and propagate the information to all expanded children.
            if (this.projectSubtree)
            {
                for (int j = this.nodes.Count - 1; j >= 0; j--)
                {
                    ExpandedProjectionNode expandedNode = this.nodes[j] as ExpandedProjectionNode;
                    if (expandedNode != null)
                    {
                        expandedNode.projectSubtree = true;
                        expandedNode.ApplyWildcardsAndSort(provider);
                    }
                }

                this.projectAllImmediateProperties = false;
                this.projectAllImmediateOperations = false;
                return;
            }

            for (int j = this.nodes.Count - 1; j >= 0; j--)
            {
                ExpandedProjectionNode expandedNode = this.nodes[j] as ExpandedProjectionNode;

                // If this node was marked to include all immediate properties,
                //   remove all children which are not expanded.
                //   That means they are either simple properties or nav. properties which
                //   are not going to be expanded anyway.
                if (this.ProjectAllImmediateProperties && expandedNode == null)
                {
                    this.nodes.RemoveAt(j);
                }
                else if (expandedNode != null)
                {
                    expandedNode.ApplyWildcardsAndSort(provider);
                }
            }

            if (this.nodes.Count > 0)
            {
                // Sort the subsegments such that they have the same order as the properties
                //   on the owning resource type.

                // build the list of existing resource types that this query touches
                List <ResourceType> resourceTypes = new List <ResourceType>();
                resourceTypes.Add(this.ResourceType);

                // If we have one or more derived properties to expand or project,
                // we need to sort it based on the order in which the derived types
                // are return.
                List <ProjectionNode> derivedProjectionNodes = this.nodes.Where(n => !ResourceType.CompareReferences(n.TargetResourceType, this.ResourceType)).ToList();
                if (derivedProjectionNodes.Count > 0)
                {
                    foreach (ResourceType rt in provider.GetDerivedTypes(this.ResourceType))
                    {
                        if (derivedProjectionNodes.FirstOrDefault(node => node.TargetResourceType == rt) != null)
                        {
                            resourceTypes.Add(rt);
                        }
                    }
                }

#if DEBUG
                int count = this.nodes.Count;
#endif
                this.nodes = ExpandedProjectionNode.SortNodes(this.nodes, resourceTypes);
#if DEBUG
                Debug.Assert(this.nodes.Count == count, "We didn't sort all the properties.");
#endif
            }
        }