Solved

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

Posted on 2011-02-21
10
399 Views
Last Modified: 2012-05-11
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
Comment
Question by:Parth48
10 Comments
 
LVL 3

Expert Comment

by:imaki06
ID: 34949141
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
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 34949151
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
 
LVL 10

Expert Comment

by:John Claes
ID: 34949343
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
 

Author Comment

by:Parth48
ID: 34949461
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
 
LVL 3

Expert Comment

by:imaki06
ID: 34949504
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:imaki06
ID: 34949559
If you want to cast that resQry to an Array. You could try something like this:

String[] qryArray= resQry.ToArray();
0
 

Author Comment

by:Parth48
ID: 34949564
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
 
LVL 3

Expert Comment

by:imaki06
ID: 34949576
Do you include:
System.Collections.Generic

What kind of an error?
0
 
LVL 3

Accepted Solution

by:
imaki06 earned 500 total points
ID: 34950111
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
 

Author Comment

by:Parth48
ID: 34950775
Thanks @imaki06:

it's work
 
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help assigning an image to an image control in my VB.Net WPF project 3 43
Runtime Error 2 28
Error in query expression 3 34
Message not shown 5 29
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now