×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to modify function GetSelectStatement to generate inner join select statement by using csharp?

How to modify function GetSelectStatement to generate inner join select statement by using csharp?

How to modify function GetSelectStatement to generate inner join select statement by using csharp?

(OP)
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 :

CODE --> sqlserver

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 :

CODE --> csharp

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;
}
I need to modify code above to generate select statement as above
to simple syntax i need to make
select FooterTable.fields from MasterTable inner join FooterTable on MasterTable.key1=FooterTable.key1 and MasterTable.key2=FooterTable.key2 and MasterTable.key3=FooterTable.key3 where key1 =value and key2=value and key3=value

How to do that please by modify code above

RE: How to modify function GetSelectStatement to generate inner join select statement by using csharp?

im not sure this is the correct answer, but it seems you are trying to generate a sql statement dynamically that joins two tables.
e.g. query = string.Format("select from table {0} inner join table{1} where", tableName);
one part of your issue might be that you seem to be using the where clause for the join columns,

When sql sees the phrase inner join/join it expects an "On" clause.
If you are planning on adding the join columns via the where clause the syntax would be
query = string.Format("select from {0},{1} where", tableName);

also query = string.Format("select from table {0} inner join table{1} where", tableName);
why do you have the word table in there?
plus you only have on tablename in the format statement, but your code is looking for 2 {0} and {1}

shouldn't it be more like
string.Format("select from {0} a inner join {1} b on a.{2} = b.{3} where", table1, table2, col1, col2);

also your original query has table {0} table{1}
table {0} has a space between the word table and the placeholder.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close