-6

I am trying to do permutations with records from the SQL Server CE database, but I have different results every time especially for 20 choose 5 = 15504

I have made 5 for loops in C# inside each other that read the date from one table

 for(int i=1;i<n;i++)
 {
         for (acv1=1; acv1 <= n; acv1++)
         { 
             string sql = "SELECT PId,DateOFVisit,RaId,VisitNum from  tblPatientVisit";
             visit1 = new ArrayList();

             if (id > 0)
             {
                    sql += "\n where PId= " + id + "AND VisitNum=" + acv1;
             }

                try
                {
                    SqlCeCommand cm1 = new SqlCeCommand(sql, f.conn);
                    SqlCeDataAdapter da1 = new SqlCeDataAdapter(cm1);

                    DataTable dt1 = new DataTable();
                    da1.Fill(dt1);

                    foreach (DataRow dr in dt1.Rows)
                    {
                        visit1.Add(dr["VisitNum"].ToString());
                        visit1.Add(dr["PId"].ToString());
                        visit1.Add(p_name);
                        visit1.Add(dr["DateOfVisit"].ToString());
                        visit1.Add(description(Convert.ToInt32(dr["RaId"])));
                        break;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                } 

     for(int x=i+1;x<n;x++)
     {
         // select statement here for x and stored it on array list b
         for(int y=x+1;y<n;y++)
         {
              // select statement here for y and stored it on array list c
              for(intz=y+1;z<n;z++)
              {
                  // select statement here for z  and stored it on array list d
                  for(int t=z+1;z<n;z++)
                  {
                       // select statement here for t and stored it on array list e
                       // in order to insert the array lists in order but i got different result than I expected
                  }
              }
          }
      }
}

I want to get the result like this

1 2 3 4 5
1 2 3 4 6
1 2 3 4 7
1 2 3 4 8
1 2 3 4 9
1 2 3 4 10
1 2 3 4 11
1 2 3 4 12
1 2 3 4 13

until

16 17 18 19 20

What I have got from the code above like this

16 2 4 6 10 

Edit :

I have 20 records on the tblACV and I want to read this record and make a permutations for the records and store them again into another table

6
  • 2
    I can't follow what you're doing. Please clean up your format and don't exclude the "select statement here" code. What's your end goal? Don't just tell us what you're doing -- tell us also what you're trying to do. There might be a better way. Nov 9, 2012 at 15:46
  • i make some edit to make it clear
    – Messi 10
    Nov 9, 2012 at 15:52
  • 1
    That hasn't made it clearer - what's the point of a load of trailing braces! Nov 9, 2012 at 15:52
  • put your code all together in one code block. Hit ctrl+K to format it and check the preview to make sure it looks formatted -- if not, hit ctrl+K again. Then, tell us what the code block does in a separate paragraph, not in-line between each line of code. And DON'T exclude the "select..." statements you're showing. I still have no clue what you're trying to achieve from your loops. Nov 9, 2012 at 15:54
  • come from the another method n= 10 or 20 depends to patient visits
    – Messi 10
    Nov 9, 2012 at 16:02

1 Answer 1

0

I am not certain that this is what you are really looking for but this would give the numbers say you are expecting:

    int n = 20;
    for ( int i = 4; i < n; i++) {
        for ( int j = 0; j < 4; j ++ ) {
            System.out.print(j + 1);
        }
        System.out.println(i + 1);
    }
1
  • I made edit on my Question thank you for your answer
    – Messi 10
    Nov 9, 2012 at 16:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.