/// <summary> /// Get an element from a table by its ID. /// </summary> /// <param name="id">The ID of the element.</param> /// <param name="parameters">A dictionary of user-defined parameters and values to include in the request URI query string.</param> /// <returns>The desired element as JSON object.</returns> private Task <JsonObject> GetSingleValueAsync(object id, IDictionary <string, string> parameters) { // Create a query for just this item string query = string.Format( CultureInfo.InvariantCulture, "$filter={0} eq {1}", MobileServiceTableUrlBuilder.IdPropertyName, TypeExtensions.ToODataConstant(id)); return(ReadAsync(query, parameters).ContinueWith(t => { // Get the first element in the response JsonObject obj = t.Result.AsObject(); if (obj == null) { JsonArray array = t.Result.AsArray(); if (array != null && array.Count > 0) { obj = array.FirstOrDefault().AsObject(); } } if (obj == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTables_GetSingleValueAsync_NotSingleObject, (t.Result ?? JsonExtensions.Null()).Stringify())); } return obj; })); }
/// <summary> /// Get an element from a table by its ID. /// </summary> /// <param name="id">The ID of the element.</param> /// <returns>The desired element as JSON object.</returns> private async Task <JsonObject> GetSingleValueAsync(object id) { // Create a query for just this item string query = string.Format( CultureInfo.InvariantCulture, "$filter={0} eq {1}", IdPropertyName, TypeExtensions.ToODataConstant(id)); // Send the query IJsonValue response = await this.ReadAsync(query); // Get the first element in the response JsonObject obj = response.AsObject(); if (obj == null) { JsonArray array = response.AsArray(); if (array != null && array.Count > 0) { obj = array.FirstOrDefault().AsObject(); } } if (obj == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTables_GetSingleValueAsync_NotSingleObject, (response ?? JsonExtensions.Null()).Stringify())); } return(obj); }
private static JsonObject FindAnswerById(JsonArray array, ulong id) => array.FirstOrDefault(a => a.ContainsKey("id") && a["id"] == id) as JsonObject;
public static void Main() { Console.WriteLine("Construct a JsonArray by parsing a JSON string"); string customers = @" [ { ""ID"" : ""538a868a-c575-4fc9-9a3e-e1e1e68c70c5"", ""Name"" : ""Yavor"", ""DOB"" : ""1984-01-17"", ""OrderAmount"" : 1e+4, ""Friends"" : [ ""007cf155-7fb4-4070-9d78-ade638df44c7"", ""91c50a40-7ade-4c37-a88f-3b7e066644dc"" ] }, { ""ID"" : ""007cf155-7fb4-4070-9d78-ade638df44c7"", ""Name"" : ""Joe"", ""DOB"" : ""1983-02-18T11:22:33Z"", ""OrderAmount"" : 50000, ""Friends"" : [ ""91c50a40-7ade-4c37-a88f-3b7e066644dc"" ] }, { ""ID"" : ""91c50a40-7ade-4c37-a88f-3b7e066644dc"", ""Name"" : ""Miguel"", ""DOB"" : ""Mon, 20 Nov 1995 19:12:08 -0500"", ""OrderAmount"" : 25.3e3, ""Friends"" : [ ""007cf155-7fb4-4070-9d78-ade638df44c7"" ] } ]"; JsonArray ja = JsonValue.Parse(customers) as JsonArray; Console.WriteLine(ja + Environment.NewLine); Console.WriteLine("Element operators - first"); var first = ja.FirstOrDefault <JsonValue>(); Console.WriteLine(first + Environment.NewLine); Console.WriteLine("Restriction operators - where"); var bigOrders = from JsonValue value in ja where value["OrderAmount"].ReadAs <int>(0) > 30000 select value; Console.WriteLine(bigOrders.Count()); bigOrders = ja.Where <JsonValue>(x => x["OrderAmount"].ReadAs <int>(0) > 30000); Console.WriteLine(bigOrders.Count()); // In some cases you are writing a LINQ query over a piece // of JSON that came from a third party and you may not be // guaranteed the shape of it. You can iterate over any // type in the JsonValue family using the KeyValuePair // iterator, and you're guaranteed that it is supported and that // it will be empty if you don't have a JsonObject or JsonArray bigOrders = from KeyValuePair <string, JsonValue> value in ja where value.Value.ValueOrDefault("OrderAmount").ReadAs <int>(0) > 30000 select value.Value; Console.WriteLine(bigOrders.Count() + Environment.NewLine); Console.WriteLine("Partition operators - skip/take"); var second = ja.Skip <JsonValue>(1).Take <JsonValue>(1); Console.WriteLine(second.Count() + Environment.NewLine); Console.WriteLine("Conversion operators"); var old = from JsonValue value in ja where value["DOB"].ReadAs <DateTime>(DateTime.Now) < new DateTime(1990, 1, 1) select value; Console.WriteLine(old.ToArray <JsonValue>().Count()); Console.WriteLine(old.ToJsonArray().Count + Environment.NewLine); Console.ReadLine(); }