又一次更新
我发现这个问题和你有同样的问题:
为什么在排序中使用随机导致[无法排序 IComparer.Compare 错误]
问题是 LINQBridgeList<>.Sort
在内部使用,它在使用“不稳定”比较算法时会抱怨,所以你很遗憾不能这样随机化。
作为替代方案,这里有一些很棒的代码来随机化或选择随机项目:
private static Random rnd = new Random();
/// <summary>
/// Chooses one of the items at random.
///
/// Returns default if there are no items.
/// </summary>
public static T RandomOrDefault<T>(this IEnumerable<T> source)
{
// We need the count:
var buffer = source as ICollection<T> ?? source.ToList(); // (iterate only once)
var itemCount = buffer.Count;
if (itemCount == 0)
{
return default(T);
}
var index = rnd.Next(itemCount);
return buffer.ElementAt(index);
}
/// <summary>
/// Randomizes the order of the elements of a sequence.
/// </summary>
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
{
// This code is an implementation of the Fisher–Yates shuffle.
// The code was obtained from:
// https://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm/1665080#1665080
T[] elements = source.ToArray();
// Note i > 0 to avoid final pointless iteration
for (int i = elements.Length - 1; i > 0; i--)
{
// Swap element "i" with a random earlier element it (or itself)
int swapIndex = rnd.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
// we don't actually perform the swap; we can forget about the
// swapped element because we already returned it.
}
// there is one item remaining that was not returned - we return it now
yield return elements[0];
}
更新
这个异常看起来真的像一个 LINQBridge 错误。 我建议更新到最新版本。 您看到此问题没有其他明显的原因。
附加信息
您可以使用 aRandom
而不是Guid
这样:
var rnd = new Random();
var result = someCollection.OrderBy(g => rnd.Next()).Take(1).FirstOrDefault();
此外,.Take(1)
绝对没有必要紧随其后.FirstOrDefault()