Exemplo n.º 1
0
        private static void WriteNavigationLinks(ODataWriter writer, object element, Uri parentEntryUri, IEdmEntityType parentEntityType, IEdmModel model, ODataVersion targetVersion, IEnumerable <string> expandedNavigationProperties)
        {
            foreach (var navigationProperty in parentEntityType.NavigationProperties())
            {
                IEdmTypeReference propertyTypeReference = navigationProperty.Type;
                bool isCollection = navigationProperty.Type.IsCollection();

                var navigationLink = new ODataNavigationLink
                {
                    Url          = new Uri(parentEntryUri, navigationProperty.Name),
                    IsCollection = isCollection,
                    Name         = navigationProperty.Name,
                };

                writer.WriteStart(navigationLink);

                if (expandedNavigationProperties.Contains(navigationProperty.Name))
                {
                    var propertyValue = DataContext.GetPropertyValue(element, navigationProperty.Name);

                    if (propertyValue != null)
                    {
                        var           propertyEntityType = propertyTypeReference.Definition as IEdmEntityType;
                        IEdmEntitySet targetEntitySet    = model.EntityContainer.EntitySets().Single(s => s.EntityType() == propertyEntityType);

                        if (isCollection)
                        {
                            WriteFeed(writer, propertyValue as IEnumerable, targetEntitySet, model, targetVersion, Enumerable.Empty <string>());
                        }
                        else
                        {
                            WriteEntry(writer, propertyValue, targetEntitySet, model, targetVersion, Enumerable.Empty <string>());
                        }
                    }
                }

                writer.WriteEnd();
            }
        }
        /// <summary>
        /// Converts a value from the data store into an ODataProperty.
        /// </summary>
        /// <param name="instance">The item from the data store.</param>
        /// <param name="propertyName">The name of the property to convert.</param>
        /// <returns>The converted ODataProperty.</returns>
        public static ODataProperty ConvertToODataProperty(object instance, string propertyName)
        {
            object value = DataContext.GetPropertyValue(instance, propertyName);

            return(CreateODataProperty(value, propertyName));
        }