我正在使用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列的C#代码(MATGB)中调整DataTable?因此,它将在Crystal Reports中的上面打印。
尝试此
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();
}
}
}
谢谢。现在我得到了答案,也喜欢与此网站分享。
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;
}