ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.1k

cannot modify function GetSelectStatement to generate select

Sep 21 2019 3:10 PM
I need to modify function GetSelectStatement to get select statment as below 
  1. select FooterTable.ItemCode,FooterTable.Quantity,FooterTable.UniPrice from  
  2.   
  3. MasterTable inner join FooterTable on MasterTable.Serial=FooterTable.Serial,MasterTable.BranchCode=FooterTable.BranchCode,MasterTable.Year=FooterTable.Year  
  4.   
  5. where MasterTable.Serial=10 AND MasterTable.Year=2019 AND MasterTable.BranchCode=1  
What i try to get result above by csharp as following :
  1. public string GetSelectStatement(string JsonDataForSelect)  
  2. {  
  3. var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);  
  4. var query = "";  
  5. var items = root.SelectToken("Details").Children().OfType().ToDictionary(p => p.Name, p => p.Value);  
  6. foreach (var item in items)  
  7. {  
  8. if (item.Key == "table")  
  9. {  
  10. var tableName = item.Value;  
  11. query = string.Format("select from table {0} inner join table{1} where", tableName);  
  12. }  
  13. else if (item.Key == "keys")  
  14. {  
  15. var key = item.Value.SelectToken("").OfType().ToDictionary(p => p.Name, p => p.Value);  
  16. var count = 0;  
  17. foreach (var id in key)  
  18. {  
  19. count++;  
  20. if (count == key.Count())  
  21. {  
  22. query += string.Format("{0} = {1}", id.Key, id.Value);  
  23. }  
  24. else  
  25. {  
  26. query += string.Format("{0} = {1} and ", id.Key, id.Value);  
  27. }  
  28. }  
  29.   
  30. }  
  31. }  
  32. return query;  
  33. }  
I need to modify code above to generate select statement as above :
 
 
How to do that please by modify code above
  1. json string i generate sql from it  
  2. {   
  3.    "Details":{   
  4.       "table":[   
  5.          "MasterTable",  
  6.          "FooterTable"  
  7.       ],  
  8.       "fields":{   
  9.          "ItemCode":"string",  
  10.          "Quantity":"int",  
  11.          "Price":"decimal"  
  12.            
  13.       },  
  14.       "keys":{   
  15.          "BranchCode":1,  
  16.          "Year":2019,  
  17.          "Serial":2  
  18.       }  
  19.    }  
  20. }  
 

Answers (1)