• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

i want to create query from string array , but how can i do this ?

string[] resultArray = new string[3];
resultArray[0] = "Option Selected 1a"
resultArray[1] = "Option Selected 2a"
resultArray[2] = "Option Selected 3d"

now i want to create query using for loop from this result array
ex.
select a from answers where ansid = 1;

in query a and ansid , i have to get it from resultarray ..

now how can i do this ??
0
Parth48
Asked:
Parth48
1 Solution
 
imaki06Commented:
Is this what you are looking for (LINQ):
string[] resultArray = new string[3];
resultArray[0] = "Option Selected 1a"
resultArray[1] = "Option Selected 2a"
resultArray[2] = "Option Selected 3d"

//look for values where number 1 is contained
IEnumerable<string> resQry = From a In resultArray Where a.indexOf('1')>-1;

Open in new window


0
 
Manoj PatilSr. Software EngineerCommented:
Hi check this one this will help you

Strring query ="select a from answers where ansid =";
for ( int cnt = 0; cnt < resultArray .Length; cnt++)
            {
                con.Open();
Strring query ="select a from answers where ansid ="+resultArray [cnt].ToString();
                cmd1 = new SqlCommand(query, con);
                cmd1.ExecuteNonQuery();
                con.Close();
            }
0
 
John ClaesCommented:
If you want 1 Query and cannot use LINQ
(Linq is prefered)


Strring query ="select a from answers where ";
String where="";
for ( int cnt = 0; cnt < resultArray .Length; cnt++)
{
     if (i>0)
     {
          where=where+" OR ";
     }
     where=where + " ansid = " + resultArray[ cnt ].ToString();
}

sqlConection.Open();
SqlCommand sqlCommand = new SqlCommand(query + where, con);
sqlCommand.ExecuteNonQuery();
sqlConection.Close();
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Parth48Author Commented:
hello @imaki06: -

ur suggestion is right and i doing it but how can i use that IEnumerable<string> ??

can u please tell me via example ??

so i can understand it better and use it in future ....
0
 
imaki06Commented:
Oh yes, this should be enough:

string[] resultArray = new string[3];
resultArray[0] = "Option Selected 1a"
resultArray[1] = "Option Selected 2a"
resultArray[2] = "Option Selected 3d"

//look for values where number 1 is contained
IEnumerable<string> resQry = From a In resultArray Where a.indexOf('1')>-1;

foreach (string result in resQry)
{
  Console.WriteLine(result);
}

Open in new window

0
 
imaki06Commented:
If you want to cast that resQry to an Array. You could try something like this:

String[] qryArray= resQry.ToArray();
0
 
Parth48Author Commented:
IEnumerable<string> resQry = From a In resultArray Where a.indexOf('1')>-1;

i got error in the above line , i include System.linq but still i got the error ??
0
 
imaki06Commented:
Do you include:
System.Collections.Generic

What kind of an error?
0
 
imaki06Commented:
There were some bugs with the casing of the letters, try this:

IEnumerable<string> resQry = from a in resultArray where a.IndexOf('1') > -1 select a;
0
 
Parth48Author Commented:
Thanks @imaki06:

it's work
 
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now