Lambda表达式按顺序排列并采取问题

the*_*mqe 10 c# linq lambda entity-framework

我有一个类类型的IQueryable列表COLOURS

IQueryable<COLOURS> renkler = dbcontext.colours.Select(s=>new COLOURS{ .... 
Run Code Online (Sandbox Code Playgroud)

我想得到随机的2行,我使用这个代码块来做到这一点:

renkler.OrderBy(o => Guid.NewGuid()).Take(2);
Run Code Online (Sandbox Code Playgroud)

我想要2行,但有时会得到3行或5行:

在此输入图像描述

Take(2) 不工作 - 问题是什么?

我检查时发现了一些东西

var result = NewProducts().OrderBy(o => Guid.NewGuid()).Take(2);
int result_count = result.Count(); //This value is 2 :D
                                   //but ToList() result 5 :D
Run Code Online (Sandbox Code Playgroud)

整个方法:

public IQueryable<COLOURS> NewProducts() 
{
    DateTime simdi = DateTime.Now;
    DateTime simdi_30 = DateTime.Now.AddDays(-30);

    var collection_products = DefaultColours()
                             .Where(w => ((w.add_date.Value >= simdi_30 && w.add_date.Value <= simdi) || w.is_new == true))
                             .OrderByDescending(o => o.add_date).Take(200)
                             .Select(s => new COLOURS
                             {
                                 colour_code = s.colour_code,
                                 model_code = s.products.model_code,
                                 sell_price = (decimal)s.sell_price,
                                 market_price = (decimal)s.market_price,
                                 is_new = (bool)s.is_new,
                                 product_id = (int)s.product_id,
                                 colour_name = s.name,
                                 product_name = s.products.name,
                                 description = s.products.description,
                                 img_path = s.product_images.FirstOrDefault(f => f.is_main == true).img_path,
                                 category_id = (int)s.category_relations.FirstOrDefault().category_id,
                                 display_order = (short)s.display_order,
                                 section_id = (int)s.products.section_id,
                                 stock_amount = s.pr_sizes.Where(w => w.is_active == true && w.quantity >= 0).Count() > 0 ? (int)s.pr_sizes.Where(w => w.is_active == true && w.quantity >= 0).Sum(s2 => s2.quantity) : 0,
                                                                      section_name = s.products.pr_sections.name,

                             });    
    return collection_products;
}

public IQueryable<COLOURS> RandomNewProducts(int n) 
{
    var result = NewProducts().OrderBy(o => Guid.NewGuid()).Take(n);
    int result_count = result.Count(); //2
    //When I run this method it's getting 5 rows               
    return result;
}
Run Code Online (Sandbox Code Playgroud)

Wae*_*her 2

这对您来说可能不是解决方案,但很难使用多行代码和图像来格式化注释。

我很确定这是您的数据提供商的问题。也许这个组件没有按照Take()它应该的方式实现。

我尝试重建您的星座,但我没有构建任何提供者,而是使用 500 个对象IQueryable构建了一个星座,并调用它来满足方法签名。List<>AsQueryable()

    public static IQueryable<COLOURS> DefaultColours()
    {
        const int COUNT = 500;

        List<COLOURS> x = new List<COLOURS>();

        var startDate = DateTime.Today.AddDays(-1 * (int)(COUNT / 2));

        // add 500 date values, and use the date and any random bool value
        for (int i = 0; i < COUNT; i++)
            x.Add(new COLOURS() { add_date = startDate.AddDays(i), is_new = i % 3 == 0 });

        return x.AsQueryable();
    }
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,该Take()方法每次总是返回两个(不同的)项目 - 正如任何人所期望的那样:

调试器视图