我有一些代码可以随机化一个提取的结果列表,但是我更喜欢不将随机变量作为提取结果的一部分,任何想法?这是我的代码:
var rnd = new Random();
var numList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var extracted = (from n1 in numList
from n2 in numList
from n3 in numList
from n4 in numList
where n1 + n2 + n3 + n4 > 20
select new { n1, n2, n3, n4, Rnd = rnd.NextDouble() })
.OrderBy(z => z.Rnd).ToList();
答案 0 :(得分:2)
如果您需要随机化输出,只需按随机值排序,而不将它们包含在所选数据中:
var rnd = new Random();
var numList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var extracted = from n1 in numList
from n2 in numList
from n3 in numList
from n4 in numList
where n1 + n2 + n3 + n4 > 20
orderby rnd.NextDouble()
select new { n1, n2, n3, n4 }; // you can use ToList()