[英]Randomly select row of data using linq statement
嗨,我有一個叫單詞表的表。 具有以下字段:WordID,Word,提示,類別。 該表有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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.