SQL随机化行和列

时间:2017-09-10 10:37:50

标签: c# mysql sql

我可以知道,我如何随机收集使用SQL收集和随机选择的数据 我将把它整合到c#winform

例如

主要内容----- content1 ----- content2 ----- content3 ----- content4

主要内容----- content1 ----- content4 ----- content2 ----- content3

像多项选择题一样,随机化问题及其选择。提前谢谢。

编辑:

这是我的班级。

public class qbank
{
    string question, c1, c2, c3, c4, ans;

    public string Ans
    {
        get { return ans; }
        set { ans = value; }
    }

    public string C1
    {
        get { return c1; }
        set { c1 = value; }
    }

    public string C2
    {
        get { return c2; }
        set { c2 = value; }
    }

    public string C3
    {
        get { return c3; }
        set { c3 = value; }
    }

    public string C4
    {
        get { return c4; }
        set { c4 = value; }
    }

    public string Question
    {
        get { return question; }
        set { question = value; }
    }

这是我的winform

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace frmMain
{
    public partial class frmPIPETest : Form
    {
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source=C:\Users\rehpe\Documents\Visual Studio 2015\Projects\panibago\questionbank.accdb;
        Persist Security Info=False";
        string query = "";
        OleDbConnection conn = null;

        public frmPIPETest()
        {
            InitializeComponent();
        }

        private void frmPIPETest_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Media.SystemSounds.Beep.Play();
            DialogResult dialog = MessageBox.Show("Do you really want to exit?",
                                  "Exit Program",
                                  MessageBoxButtons.YesNo,
                                  MessageBoxIcon.Question,
                                  MessageBoxDefaultButton.Button2);
            if (dialog == DialogResult.Yes)
            {
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            System.Media.SystemSounds.Beep.Play();
            DialogResult dialog = MessageBox.Show("Go back to topic selection?",
                                  "Return",
                                  MessageBoxButtons.YesNo,
                                  MessageBoxIcon.Question,
                                  MessageBoxDefaultButton.Button2);
            if (dialog == DialogResult.Yes)
            {
                frmPIPE frm = new frmPIPE();
                frm.Show();
                Hide();
            }
        }

        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            lblTimer.Text = i.ToString() + "s";
        }

        public qbank()
        {
            IEnumerable<qbank> content = new List<qbank>
            var random = new Random();
            var result = content
                .Select(c =>
                {
                    var strings = new[]
                    {
                        c.C1,
                        c.C2,
                        c.C3,
                        c.C4,
                    }.OrderBy(x => random.Next())
                    .ToArray();
                    return new qbank
                    {
                        Question = c.Question,
                        C1 = strings[0],
                        C2 = strings[1],
                        C3 = strings[2],
                        C4 = strings[3],
                    };
                })
            .OrderBy(x => random.Next());
        }
    }
}

我附上图片,因为我创建列表的地方有错误。

Expected Interface

2 个答案:

答案 0 :(得分:1)

您可以在mysql查询中通过rand()订购:

示例:

SELECT * FROM content ORDER BY rand() 

<强> RAND([N])

  

返回0&lt; = v&lt;范围内的随机浮点值v。 1.0。至   获得i <= R&lt;范围内的随机整数R. j,使用表达式   FLOOR(i + RAND()*(j - i))。

答案 1 :(得分:1)

我假设您的代码中已经有DTO类,如下所示:

public class Table
{
    public string MainContent { get; set; }
    public string Content1 { get; set; }
    public string Content2 { get; set; }
    public string Content3 { get; set; }
    public string Content4 { get; set; }
}

现在您可以使用Linq:

随机化行和列
IEnumerable<Table> content = /* get it from DbContext */;
var random = new Random();

var result = content
    .Select(c =>
    { 
        var strings = new[]
            {
                c.Content1,
                c.Content2,
                c.Content3,
                c.Content4,
            }.OrderBy(x => random.Next()) // randomize columns
            .ToArray();

        return new Table
        {
            MainContent = c.MainContent,
            Content1 = strings[0],
            Content2 = strings[1],
            Content3 = strings[2],
            Content4 = strings[3],
        };
    })
    .OrderBy(x => random.Next());  // randomize rows

注意:如果您的表格较大,请在第一个Where之前添加过滤(Skip)和分页(TakeSelect),如下所示:

.Where(t => t.MainContent.Contains("abc"))
.Skip(100)
.Take(10)
.AsEnumerable()

编辑:您可以找到工作示例here