【问题标题】:SQL randomize rows and columnsSQL 随机化行和列
【发布时间】:2017-09-10 10:37:49
【问题描述】:

请问,如何随机化收集的数据并随机化使用 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

【问题讨论】:

  • 为什么不在 C# 中随机化呢?
  • 内容是通过SQL生成的吗?可以和我们分享一下代码吗?
  • @Odonno 我如何在 c# 中随机化?对不起
  • @sagi 我有一个连接到我的 winform 的数据库(访问)。我仍然没有代码,因为我不知道如何查询它。我试图查询随机数据。但我所能做的就是随机化它的行,我也想随机化选定的列。
  • @JepherJohnChang 这是一个例子 (stackoverflow.com/questions/5383498/…)。您可能需要创建一个包含所有选择的 List,然后使用方法对其进行随机化。

标签: c# mysql sql


【解决方案1】:

你可以在mysql查询中通过rand()排序:

示例:

SELECT * FROM content ORDER BY rand() 

兰德([N])

返回 0

【讨论】:

  • 是的,我试过了。但它只能随机化行。我不仅要随机化行,还要随机化该行中的列。
  • @JepherJohnChang 。 . .随机化应用层中的列。
  • @GordonLinoff 怎么样?
【解决方案2】:

我假设您的代码中已经有一个 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

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

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

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

【讨论】:

  • 是的,我有一个 dto 课程。抱歉,我迷失在你的 x、t 和 c 变量上。因为它们没有被宣布?还是我必须将它们设置为我的条件?
  • @JepherJohnChang x, t anc c 是一个 lambda 参数。我添加了演示链接。
  • 谢谢。我一回到家就回到我的代码。
  • 这不会随机化我相信的列
  • @AbdulSamad 我已经在内存集合上测试了这段代码,它工作正常。当这段代码不起作用时,你能给我一个例子吗?
猜你喜欢
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2020-05-02
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多