5 Replies - 316 Views - Last Post: 24 September 2019 - 12:15 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

How to generate select statement from function GetSelectStatement ?

Posted 21 September 2019 - 02:59 PM

How to generate inner select statement from function GetSelectStatement using csharp ?

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

function i need to modify it

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;
        }


{ 
           "Details":{ 
              "table":[ 
                 "MasterTable",
                 "FooterTable"
              ],
              "fields":{ 
                 "ItemCode":"string",
                 "Quantity":"int",
                 "Price":"decimal"

              },
              "keys":{ 
                 "BranchCode":1,
                 "Year":2019,
                 "Serial":2
              }
           }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: How to generate select statement from function GetSelectStatement ?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7135
  • View blog
  • Posts: 24,240
  • Joined: 05-May 12

Re: How to generate select statement from function GetSelectStatement ?

Posted 21 September 2019 - 04:41 PM

Also posted in C# Forums...
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7135
  • View blog
  • Posts: 24,240
  • Joined: 05-May 12

Re: How to generate select statement from function GetSelectStatement ?

Posted 21 September 2019 - 09:54 PM

And also cross posted in CodeProject and ASP.NET.
Was This Post Helpful? 0
  • +
  • -

#4 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: How to generate select statement from function GetSelectStatement ?

Posted 22 September 2019 - 01:20 AM

Are make more posting on more forum is wrong thing or bad
every thing may be solved by more way
on last this is share information for others
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7135
  • View blog
  • Posts: 24,240
  • Joined: 05-May 12

Re: How to generate select statement from function GetSelectStatement ?

Posted 22 September 2019 - 05:58 AM

View Postahmedba, on 22 September 2019 - 04:20 AM, said:

Are make more posting on more forum is wrong thing or bad

Yes. It is considered poor netiquette to spam a question to multiple forums. See the Choose your Forum Carefully section of "How to Ask Questions the Smart Way".

View Postahmedba, on 22 September 2019 - 04:20 AM, said:

every thing may be solved by more way
on last this is share information for others

If that is true, I have yet to see you reply back to all your crossposted questions and share the solutions. You seem to just selfishly take and take and take, but never contribute back by sharing the answers.
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2996
  • View blog
  • Posts: 11,539
  • Joined: 03-December 12

Re: How to generate select statement from function GetSelectStatement ?

Posted 24 September 2019 - 12:15 PM

Is there a reason you are using dynamic statements rather than a repository pattern for the queries to begin with?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1