【问题标题】:How to shuffle the Particular column of Datatable in c#如何在c#中打乱Datatable的特定列
【发布时间】:2017-04-18 03:17:28
【问题描述】:

我正在使用 MS Access 数据库开发 C# 中的考试试卷匹配模块。这里有一个与匹配模块相关的问题:我有一个如下所示的数据表,我想以随机播放的形式调用匹配模块。

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 中打印出来。

【问题讨论】:

    标签: c# ms-access datatable crystal-reports dataset


    【解决方案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();
    
            }
        }
    }
    

    【讨论】:

    • 感谢您的代码。但是没有 linq 可以吗?
    • 是的,它只是需要更多的代码行。在这种情况下,我喜欢 linq,因为在没有 linq 的情况下更容易理解代码。如果没有 Linq,您需要创建一个 List> 然后对结果进行排序。
    【解决方案2】:

    谢谢。现在我得到了答案,也想和这个网站分享。

         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;
            }
    

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      相关资源
      最近更新 更多