using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
Your LINQ query can be implemented as follows:
var city = (string)json["departments"]
.Where(o => o[id] != null)
.Select(o => o[id]["city"]).SingleOrDefault();
Alternatively, you could use [`SelectToken()`](https:
var path = $"departments[*].{id}.city";
var city = (string)json.SelectToken(path);
`SelectToken()` supports [JSONPath syntax](https:
Demo fiddle [here](https:
public static void Test()
var json = JToken.Parse(GetJson());
json["departments"].Single(x=>x.Name==id.ToString())["city"];
""2"": {""city"": ""Petersburg"", ""employees"": ""1200""}
""1"": {""city"": ""Ekaterinburg"", ""employees"": ""4000""}
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];