I am developing the matching module of Exam question paper in C# with a MS Access database. Here comes an issue related to matching module: I have a datatable like below and I want to call the matching module in shuffle form.
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
Question is printing correctly in report but stuck in matching.
I executing the following query.
Query
Select *
from Question_table
where std = 1 and sub = "eng"
Crystal Report output:
Match the following :
1.Lion Den
2.hen Coop
3.Rabbit Burrow
But I want an output like for matching :
Match the following :
1.Lion Burrow
2.hen Den
3.Rabbit Coop
My question is how would shuffle the datatable in C# code of particular 1 column (MatchB) ? So it will print like above in Crystal Reports.
Try this
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();
}
}
}
Thanks . now i got the answer and also like to share with this site .
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;
}
}
}
Random Function which generate the numbers from min to max value of rows and call only once
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.