使用linq语句随机选择一行数据
本文关键字:一行 数据 linq 语句 随机 选择 使用 | 更新日期: 2023-09-27 18:02:36
你好,我有一个名为word table的表。包含以下字段:单词,单词,提示,分类。该表有40行数据。我想随机选择一行符合特定标准的数据。下面是我的代码:
public List<WordTable> get(string diff, string cat)
{
using(Entities obj = new Entities())
{
var qry = (from c in obj.WordTables where c.DifficultyLevel == diff
&& c.Category == cat select c);
return qry.ToList();
}
}
对于随机选择,您可以这样做,
public WordTable get(string diff, string cat)
{
var qry = (from c in obj.WordTables where
c.DifficultyLevel == diff && c.Category == cat select c);
var list = qry.ToList();
Random r = new Random()
var element = list [ r.Next(0, list.count-1)];
return element;
}