使用 LinqToExcel 时无法随机排序

Can't random orderby while using LinqToExcel

我正在使用由 MIT 开发并托管在 Google Code at http://code.google.com/p/linqtoexcel/wiki/UsingLinqToExcel 上的 LinqToExcel 项目来解析来自 excel table 的数据集,看起来像这样:

Part of the dataset

我正在尝试使用 Linq OrderBy 随机数技术从包含面积和长度的数据集中随机选择 30 excel 行:

public class Obj
{
    public double Area { get; set; }
    public double Length { get; set; }
}

namespace LinqToExcel.Screencast
{
    class Program
    {
        static void Main()
        {
            var excel = new ExcelQueryFactory();
            excel.FileName = @"C:\radno\jame.xls";

            Random rnd = new Random();

            var listExcel = from x in excel.Worksheet<Obj>()
                select x;

            var orderedList = listExcel.OrderBy(r => rnd.Next());

            foreach (var u in orderedList.Take(30))
            {
                Console.WriteLine($"the area is {u.Area} while length is {u.Length}");
            }
        }
    }
}

但这不起作用,因为我总是得到相同的前 30 双。我在这里做错了什么吗?这应该有效吗?这是 LinqToExcel 错误吗?

我已经尝试了 list.OrderBy(x => Guid.NewGuid()) 技巧,但我得到了相同的结果。

看起来 OrderBy 只有在您指向确切的列时才能工作。我检查了图书馆的来源。如果你去: https://github.com/paulyoder/LinqToExcel/blob/master/src/LinqToExcel/Query/SqlGeneratorQueryModelVisitor.cs(第 113 行) 您可以看到什么被带到 OrderBy SQL。它忽略任何方法调用并仅保存列名。它不会在这种方法中起作用。作为解决方法,您可以在 .OrderBy().

之前执行 .ToList()