Вот простой способ, как показано ниже:
public string GetSelectStatement(string JsonDataForSelect)
{
var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);
var query = "";
var query1 = "";
var query2 = "";
string[] tableName = new string[] { };
var items = root.SelectToken("Details").Children().OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var item in items)
{
if (item.Key == "table")
{
tableName = JsonConvert.DeserializeObject<string[]>(item.Value.ToString());
}
else if (item.Key == "fields")
{
var key = item.Value.SelectToken("").OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
var count = 0;
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query1 += string.Format("{0}.{1}", tableName[1], id.Key);
}
else
{
query1 += string.Format("{0}.{1},", tableName[1], id.Key);
}
query = string.Format("select {0} from {1} inner join {2} on {3} where ", query1, tableName[0], tableName[1], query2);
}
}
else if (item.Key == "keys")
{
var key = item.Value.SelectToken("").OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
var count = 0;
if (query2 == "")
{
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query2 += string.Format("{0}.{1}={2}.{3}", tableName[0], id.Key, tableName[1], id.Key);
}
else
{
query2 += string.Format("{0}.{1}={2}.{3},", tableName[0], id.Key, tableName[1], id.Key);
}
query = string.Format("select {0} from {1} inner join {2} on {3} where ", query1, tableName[0], tableName[1], query2);
}
}
count = 0;
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query += string.Format("{0}.{1}={2} ", tableName[0], id.Key, id.Value);
}
else
{
query += string.Format("{0}.{1}={2} and ", tableName[0], id.Key, id.Value);
}
}
}
}
return query;
}
Результат: