I'm trying to exclude some column while executing the select query:
string query=@" select c.id,person_id,c.customer_no,c.status,p.first_name,
p.last_name,p.dob as DateOfBirth
from customer c
inner join person p on p.id=c.person_id";
query = Regex.Replace(query, @"\t|\n|\r", "");
int pFrom = query.IndexOf("select ") + "select ".Length;
int pTo = query.LastIndexOf("from ");
String commaSeperatedFields = query.Substring(pFrom, pTo - pFrom);
string[] IgnoredFields = new string[]{"id", "person_id","DateOfBirth"};
var fieldsWithAlias=commaSeperatedFields.Split(',');
var fields=fieldsWithAlias.Select(x=>(x.Contains(".")?x.Split('.')[1]:x));
fields=fields.Select(x=>(x.Contains(" as ")?x.Split(new string[] { " as " }, StringSplitOptions.None)[1]:x));
var resultArray=fields.Except(IgnoredFields);
var result =String.Join(",",resultArray);
var customQuery=@"select "+result +" from ("+query+")trimmed" ;
Console.WriteLine(customQuery);
Its working as expected, Now I need to add full name by using concat function but I don't know how to extract concated entire field.
Complex Query:
string query=@" select c.id,person_id,c.customer_no,c.status,p.first_name,p.last_name,
p.dob as DateOfBirth,concat(p.first_name,' ',p.last_name) as fullName
from customer c
inner join person p on p.id=c.person_id";