从表中选择随机记录,并在C#代码

时间:2016-05-11 21:41:00

标签: c# sql lambda

我正在编写一个小进程,我可以从表中选择随机记录,其中带有复选框/布尔值的字段也是未选中的。 故事的寓意是选择随机记录,使用它并设置checkbox / boolean field == Checked。

我通过从Count()方法中选择随机记录来接近这项工作,我得到了Integer作为回报,我可以从该池中选择随机数。但我还需要过滤查询以仅选择未选中复选框/布尔字段的记录,但我还没有通过以下代码管理到目前为止。

我使用Lambda表达式如下,但可能存在更好的方法。

 public class Program
{
    static void Main(string[] args)
    {
        DatabaseModelContainer mc = new DatabaseModelContainer();
        MyTable myTable = new MyTable();
        Random rnd = new Random();
        int index = rnd.Next(myTable.Count);

        string selectedRec = myTable.MyField.OrderBy(r => myTable.MyField).Take(index).ToString();

        //Selected record to be shown at the screen, but will not appear (only reference point is shown) 
        Console.WriteLine(selectedRec);

        //Selected random value is displayed here and that indeed worked, I get random value there, but I can't use the id to select record above based on that id.
        Console.WriteLine(index.ToString());

        Console.ReadLine();

    }
}

上面的代码没有显示选择随机记录的人WHERE CheckboxField == Checked,因此也需要添加代码。

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

假设您的MyTable是一个序列,正确的做法应该是:

var randomRecord = myTable
    .Where(x => x.CheckboxField)
    .ToList()
    .OrderBy(x => rnd.Next())
    .FirstOrDefault();

完整示例:

请在此处查看:https://dotnetfiddle.net/B4JLU0

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{

    class TableRow 
    {
        public int I {get;set;}
        public bool CheckboxField {get;set;}

        TableRow(int i)
        {
            this.I = i;
            this.CheckboxField = (i % 3 == 0); //in our demo, assume that some are already checked, some aren't
        }

        public static IEnumerable<TableRow> GenerateTableRows(int howMany)
        {
            for (int i=0; i<howMany; i++)
            {
                yield return new TableRow(i);
            }
        }

    }

    public static void Main()
    {
        new Program();
        Console.WriteLine("Done");
    }

    Program() 
    {
        ICollection<TableRow> myTable = new List<TableRow>(TableRow.GenerateTableRows(12));
        while (myTable.Count(x => !x.CheckboxField) > 0) //keep looping until we've processed all records
        {
            var randomRecord = GetRandomRecord(myTable);
            Console.WriteLine(randomRecord == null ? "No Matching Rows Exist" : randomRecord.I.ToString()); 
        }
    }

    TableRow GetRandomRecord(ICollection<TableRow> myTable)
    {
        var rnd = new Random(); //we could optionally provide this as a parameter too to avoid recreating each time
        var randomRecord = myTable
            .Where(x => !x.CheckboxField)
            .ToList()
            .OrderBy(x => rnd.Next())
            .FirstOrDefault();  
        if(randomRecord != null) //avoid issue if all items are checked
        {
            randomRecord.CheckboxField = true; //mark this record as having been processed
        }
        return randomRecord;
    }

}

答案 1 :(得分:0)

应该能够做到这一点:

var randomNumberGenerator = new Random();
var randomNumber = randomNumberGenerator.Next(myTable.Count(r => r.CheckboxField)) - 1;

myTable
    .Where(r => r.CheckboxField)
    .Skip(randomNumber)
    .Select(r => r.DesiredColumn)
    .FirstOrDefault();