ahmedsalah
Active member
- Joined
- Sep 26, 2018
- Messages
- 32
- Programming Experience
- 3-5
I work on csharp and i need to generate select statement based on inner join select statement but i cannot modify it
I need to get fields and keys and table to generate inner join select statement as below :
select FooterTable.ItemCode,FooterTable.Quantity,FooterTable.UniPrice from
MasterTable inner join FooterTable on MasterTable.Serial=FooterTable.Serial,MasterTable.BranchCode=FooterTable.BranchCode,MasterTable.Year=FooterTable.Year
where MasterTable.Serial=10 AND MasterTable.Year=2019 AND MasterTable.BranchCode=1
What i try to get result above by csharp as following :
public string GetSelectStatement(string JsonDataForSelect)
{
var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);
var query = "";
var items = root.SelectToken("Details").Children().OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var item in items)
{
if (item.Key == "table")
{
var tableName = item.Value;
query = string.Format("select from table {0} inner join table{1} where", tableName);
}
else if (item.Key == "keys")
{
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())
{
query += string.Format("{0} = {1}", id.Key, id.Value);
}
else
{
query += string.Format("{0} = {1} and ", id.Key, id.Value);
}
}
}
}
return query;
}
json string i generate sql from it
{
"Details":{
"table":[
"MasterTable",
"FooterTable"
],
"fields":{
"ItemCode":"string",
"Quantity":"int",
"Price":"decimal"
},
"keys":{
"BranchCode":1,
"Year":2019,
"Serial":2
}
}
}
I need to get fields and keys and table to generate inner join select statement as below :
select FooterTable.ItemCode,FooterTable.Quantity,FooterTable.UniPrice from
MasterTable inner join FooterTable on MasterTable.Serial=FooterTable.Serial,MasterTable.BranchCode=FooterTable.BranchCode,MasterTable.Year=FooterTable.Year
where MasterTable.Serial=10 AND MasterTable.Year=2019 AND MasterTable.BranchCode=1
What i try to get result above by csharp as following :
public string GetSelectStatement(string JsonDataForSelect)
{
var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);
var query = "";
var items = root.SelectToken("Details").Children().OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var item in items)
{
if (item.Key == "table")
{
var tableName = item.Value;
query = string.Format("select from table {0} inner join table{1} where", tableName);
}
else if (item.Key == "keys")
{
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())
{
query += string.Format("{0} = {1}", id.Key, id.Value);
}
else
{
query += string.Format("{0} = {1} and ", id.Key, id.Value);
}
}
}
}
return query;
}
json string i generate sql from it
{
"Details":{
"table":[
"MasterTable",
"FooterTable"
],
"fields":{
"ItemCode":"string",
"Quantity":"int",
"Price":"decimal"
},
"keys":{
"BranchCode":1,
"Year":2019,
"Serial":2
}
}
}
Last edited: