public static string EntityReferenceTostring(EntityReference entityReference, WebApiMetadata webApiMetadata)
        {
            string logicalName   = entityReference.LogicalName.ToLower();
            string entitySetName = webApiMetadata.GetEntitySetName(logicalName);

            if (entityReference.KeyAttributes?.Any() == true)
            {
                IEnumerable <string> keys = entityReference.KeyAttributes.Select(s => $"{s.Key}='{s.Value.ToString().Replace("'", "''")}'");
                return($"{entitySetName}({string.Join("&",keys)})");
            }

            return($"/{entitySetName}{entityReference.Id:P}");
        }
        public static string GetEntityApiUrl(Entity entity, WebApiMetadata webApiMetadata)
        {
            string entitySetName = webApiMetadata.GetEntitySetName(entity.LogicalName);

            if (entity.KeyAttributes?.Any() == true)
            {
                IEnumerable <string> keys = entity.KeyAttributes.Select(s => $"{s.Key}='{s.Value.ToString().Replace("'", "''")}'");
                return($"{entitySetName}({string.Join("&",keys)})");
            }

            if (entity.Id != Guid.Empty)
            {
                return(entitySetName + entity.Id.ToString("P"));
            }

            return(entitySetName);
        }
        public override void WriteJson(JsonWriter writer, EntityReference value, JsonSerializer serializer)
        {
            string logicalName   = value.LogicalName.ToLower();
            string entitySetName = webApiMetadata.GetEntitySetName(logicalName);

            string reference;

            if (value.KeyAttributes?.Any() == true)
            {
                IEnumerable <string> keys = value.KeyAttributes
                                            .Select(s => $"{s.Key}='{s.Value.ToString().Replace("'", "''")}'");
                reference = $"{entitySetName}({string.Join("&", keys)})";
            }
            else
            {
                reference = $"/{entitySetName}{value.Id:P}";
            }

            writer.WriteValue(reference);
        }