private IEdmElement GetFunctionImportAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var operationImport = (element as IEdmOperationImport);

            if (null != operationImport)
            {
                if (targetType.Equals(AnnotatableElementType.Parameter))
                {
                    return(operationImport.Operation.FindParameter(targetName));
                }
            }

            return(null);
        }
        private IEdmElement GetComplexTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var complexType = (element as IEdmComplexType);

            if (null != complexType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return(complexType.FindProperty(targetName));
                }
            }

            return(null);
        }
        private IEdmElement GetEntityTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var entityType = (element as IEdmEntityType);

            if (null != entityType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return(entityType.FindProperty(targetName));
                }
                else if (targetType.Equals(AnnotatableElementType.NavigationProperty))
                {
                    var navigationProperties = entityType.NavigationProperties().Where(n => n.Name.Equals(targetName));
                    return(navigationProperties.Count() == 0 ? null : navigationProperties.First());
                }
            }

            return(null);
        }
        private IEdmElement GetEntityContainerAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var entityContainer = (element as IEdmEntityContainer);

            if (null != entityContainer)
            {
                if (targetType.Equals(AnnotatableElementType.EntitySet))
                {
                    return(entityContainer.FindEntitySet(targetName));
                }
                else if (targetType.Equals(AnnotatableElementType.FunctionImport))
                {
                    var operationImports = entityContainer.FindOperationImports(targetName);
                    return(operationImports == null || operationImports.Count() == 0 ? null : operationImports.First());
                }
            }

            return(null);
        }
        private IEdmElement GetEdmModelAnnotatableElement(string targetNamespace, string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var model = (element as IEdmModel);

            if (null != model)
            {
                if (targetType.Equals(AnnotatableElementType.ComplexType) || targetType.Equals(AnnotatableElementType.Enum))
                {
                    return(model.FindType(this.GetFullName(targetNamespace, targetName)));
                }
                else if (targetType.Equals(AnnotatableElementType.EntityType))
                {
                    return(model.FindEntityType(this.GetFullName(targetNamespace, targetName)));
                }
                else if (targetType.Equals(AnnotatableElementType.Action))
                {
                    var functions = model.FindOperations(this.GetFullName(targetNamespace, targetName));
                    return(functions == null || functions.Count() == 0 ? null : functions.First());
                }
                else if (targetType.Equals(AnnotatableElementType.Function))
                {
                    var functions = model.FindOperations(this.GetFullName(targetNamespace, targetName));
                    return(functions == null || functions.Count() == 0 ? null : functions.First());
                }
                else if (targetType.Equals(AnnotatableElementType.Term))
                {
                    return(model.FindTerm(this.GetFullName(targetNamespace, targetName)));
                }
                else if (targetType.Equals(AnnotatableElementType.EntityContainer))
                {
                    return(model.FindEntityContainer(targetName));
                }
            }

            return(null);
        }
        private IEdmElement GetFunctionImportAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var operationImport = (element as IEdmOperationImport);

            if (null != operationImport)
            {
                if (targetType.Equals(AnnotatableElementType.Parameter))
                {
                    return operationImport.Operation.FindParameter(targetName);
                }
            }

            return null;
        }
        private IEdmElement GetAnnotatableElement(string targetNamespace, string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            if (null != (element as IEdmModel))
            {
                return(this.GetEdmModelAnnotatableElement(targetNamespace, targetName, targetType, element));
            }
            else if (null != (element as IEdmEntityContainer))
            {
                return(this.GetEntityContainerAnnotatableElement(targetName, targetType, element));
            }
            else if (null != (element as IEdmEntityType))
            {
                return(this.GetEntityTypeAnnotatableElement(targetName, targetType, element));
            }
            else if (null != (element as IEdmComplexType))
            {
                return(this.GetComplexTypeAnnotatableElement(targetName, targetType, element));
            }
            else if (null != (element as IEdmOperation))
            {
                return(this.GetFunctionAnnotatableElement(targetName, targetType, element));
            }
            else if (null != (element as IEdmOperationImport))
            {
                return(this.GetFunctionImportAnnotatableElement(targetName, targetType, element));
            }

            return(null);
        }
        private IEdmElement GetComplexTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var complexType = (element as IEdmComplexType);

            if (null != complexType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return complexType.FindProperty(targetName);
                }
            }

            return null;
        }
        private IEdmElement GetEntityTypeAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var entityType = (element as IEdmEntityType);

            if (null != entityType)
            {
                if (targetType.Equals(AnnotatableElementType.Property))
                {
                    return entityType.FindProperty(targetName);
                }
                else if (targetType.Equals(AnnotatableElementType.NavigationProperty))
                {
                    var navigationProperties = entityType.NavigationProperties().Where(n => n.Name.Equals(targetName));
                    return navigationProperties.Count() == 0 ? null : navigationProperties.First();
                }
            }

            return null;
        }
        private IEdmElement GetEntityContainerAnnotatableElement(string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var entityContainer = (element as IEdmEntityContainer);

            if (null != entityContainer)
            {
                if (targetType.Equals(AnnotatableElementType.EntitySet))
                {
                    return entityContainer.FindEntitySet(targetName);
                }
                else if (targetType.Equals(AnnotatableElementType.FunctionImport))
                {
                    var operationImports = entityContainer.FindOperationImports(targetName);
                    return operationImports == null || operationImports.Count() == 0 ? null : operationImports.First();
                }
            }

            return null;
        }
        private IEdmElement GetEdmModelAnnotatableElement(string targetNamespace, string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            var model = (element as IEdmModel);

            if (null != model)
            {
                if (targetType.Equals(AnnotatableElementType.ComplexType) || targetType.Equals(AnnotatableElementType.Enum))
                {
                    return model.FindType(this.GetFullName(targetNamespace, targetName));
                }
                else if (targetType.Equals(AnnotatableElementType.EntityType))
                {
                    return model.FindEntityType(this.GetFullName(targetNamespace, targetName));
                }
                else if (targetType.Equals(AnnotatableElementType.Action))
                {
                    var functions = model.FindOperations(this.GetFullName(targetNamespace, targetName));
                    return functions == null || functions.Count() == 0 ? null : functions.First();
                }
                else if (targetType.Equals(AnnotatableElementType.Function))
                {
                    var functions = model.FindOperations(this.GetFullName(targetNamespace, targetName));
                    return functions == null || functions.Count() == 0 ? null : functions.First();
                }
                else if (targetType.Equals(AnnotatableElementType.ValueTerm))
                {
                    return model.FindValueTerm(this.GetFullName(targetNamespace, targetName));
                }
                else if (targetType.Equals(AnnotatableElementType.EntityContainer))
                {
                    return model.FindEntityContainer(targetName);
                }
            }

            return null;
        }
        private IEdmElement GetAnnotatableElement(string targetNamespace, string targetName, AnnotatableElementType targetType, IEdmElement element)
        {
            if (null != (element as IEdmModel)) 
            {
                return this.GetEdmModelAnnotatableElement(targetNamespace, targetName, targetType, element);
            }
            else if (null != (element as IEdmEntityContainer)) 
            {
                return this.GetEntityContainerAnnotatableElement(targetName, targetType, element);
            }
            else if (null != (element as IEdmEntityType))
            {
                return this.GetEntityTypeAnnotatableElement(targetName, targetType, element);
            }
            else if (null != (element as IEdmComplexType))
            {
                return this.GetComplexTypeAnnotatableElement(targetName, targetType, element);
            }
            else if (null != (element as IEdmOperation))
            {
                return this.GetFunctionAnnotatableElement(targetName, targetType, element);
            }
            else if (null != (element as IEdmOperationImport))
            {
                return this.GetFunctionImportAnnotatableElement(targetName, targetType, element);
            }

            return null;
        }