2016-12-03 59 views
0

我正在開發與MS Access數據庫在C#中的考試問題論文的匹配模塊。這裏有一個與匹配模塊相關的問題:我有一個像下面這樣的數據表,我想以shuffle的形式調用匹配模塊。如何洗牌C#中的Datatable的特定列

Q_id   Question   Question_type   MatchA  MatchB Std sub 

1   Where is Lion live?  Ques_Ans         1 eng            
2   What is sun ?    Ques_Ans         1 eng 
3    NULL     Matching   Lion  Den   1 eng 
4    NULL     Matching   Hen  Coop   1 eng 
5    NULL     Matching   Rabbit  Burrow  1 eng 
6    NUll     Matching   Earth  Planet  2 Sci 

問題在報告中正確打印,但卡在匹配中。

我正在執行以下查詢。

查詢

Select * 
from Question_table 
where std = 1 and sub = "eng" 

水晶報表輸出:

Match the following : 

    1.Lion   Den 
    2.hen   Coop 
    3.Rabbit  Burrow 

但我要像匹配的輸出:

Match the following : 

1.Lion   Burrow 
2.hen   Den 
3.Rabbit  Coop 

我的問題是如何將洗牌的數據表中特定1列(MatchB)的C#代碼?所以它會在Crystal Reports中像上面那樣打印。

回答

0

感謝。現在我得到了答案,也喜歡與本網站分享。

 int cnt_match = ds.Tables[0].Select("Question_type = Matching").Length; //Count the numbers of row which having question matching . 
      int count = 0; 
      int min = 0; 
      int max = 0; 
//Make a list of random number 
      List<int> list_rno = new List<int>(); //random number list array 
      if (cnt_match > 0) 
      { 
       //- shuffling the match B ---> Start 
       Random rand = new Random(); 
       for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
       { 
        string que_type = ds.Tables[0].Rows[i]["Question_Type"].ToString(); 
        if (que_type == "Matching") 
        { 
         if (count == 0) 
         { 
          max = i + cnt_match;  //Last row of dataset of matching question 
          min = i; //First row no of Dataset of matching question 
          list_rno = GetRandomNumbers(min, max); 
         } 
         if (count < cnt_match) 
         { 
          //swapping the value <--start--> 
          string temp = ds.Tables[0].Rows[i]["MatchB"].ToString(); 
          ds.Tables[0].Rows[i]["MatchB"] = ds.Tables[0].Rows[list_rno[count]]["MatchB"].ToString(); 
          ds.Tables[0].Rows[list_rno[count]]["MatchB"] = temp; 
          //swaping the value <--end--> 
          count++; 
         } 
         else 
         { 
          break; 
         } 
        } 
       } 

其生成從最小的號碼排的最大值,並調用隨機函數只有一次

static Random random = new Random(); 
     public static List<int> GetRandomNumbers(int min, int max) 
     { 
      List<int> randomNumbers = new List<int>(); 
      for (int i = min; i < max; i++) 
      { 
       int number; 
       do 
       { 
        number = random.Next(min, max); 
       } 
       while (randomNumbers.Contains(number)); 
       randomNumbers.Add(number); 
      } 
      return randomNumbers; 
     } 
1

試試這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      DataTable dt = new DataTable(); 

      dt.Columns.Add("Q_id", typeof(int)); 
      dt.Columns.Add("Question", typeof(string)); 
      dt.Columns.Add("Question_type", typeof(string)); 
      dt.Columns.Add("MatchA", typeof(string)); 
      dt.Columns.Add("MatchB", typeof(string)); 
      dt.Columns.Add("Std", typeof(int)); 
      dt.Columns.Add("sub", typeof(string)); 

      dt.Rows.Add(new object[] { 1, "Where is Lion live?", "Ques_Ans", null, null, 1, "eng"}); 
      dt.Rows.Add(new object[] { 2, "What is sun ?", "Ques_Ans", null, null, 1, "eng"}); 
      dt.Rows.Add(new object[] { 3, null, "Matching", "Lion", "Den", 1, "eng"}); 
      dt.Rows.Add(new object[] { 4, null, "Matching", "Hen", "Coop", 1, "eng"}); 
      dt.Rows.Add(new object[] { 5, null, "Matching", "Rabbit", "Burrow", 1, "eng"}); 
      dt.Rows.Add(new object[] { 6, null, "Matching", "Earth", "Planet", 2, "Sci"}); 

      List<DataRow> questions = dt.AsEnumerable().Where(x => x.Field<string>("Question_type") == "Ques_Ans").ToList(); 

      List<DataRow> answers = dt.AsEnumerable().Where(x => x.Field<string>("Question_type") == "Matching").ToList(); 

      Random rand = new Random(); 
      foreach(DataRow question in questions) 
      { 
       List<DataRow> questionAnswers = answers.Where(x => x.Field<int>("Std") == question.Field<int>("Q_id")).ToList(); 

       //create random list of Match B 

       var randB = questionAnswers.Select(x => new { B = x.Field<string>("MatchB"), rand = rand.Next() }).OrderBy(x => x.rand).ToList(); 
       Console.WriteLine("Question {0}", question.Field<string>("Question")); 
       for(int i = 0; i < questionAnswers.Count; i++) 
       { 
        Console.WriteLine("{0}. {1}  {2}", i + 1, questionAnswers[i].Field<string>("MatchA"), randB[i].B); 
       } 
      } 
      Console.ReadLine(); 

     } 
    } 
} 
+0

感謝您的代碼。但有沒有可能做linq? – Mamta

+0

是的,它只需要更多的代碼行。在這種情況下,我喜歡linq,因爲它更容易理解代碼,然後在沒有linq的情況下執行代碼。如果沒有Linq,你需要創建一個List >,然後對結果進行排序。 – jdweng