如何在linq中获得随机行,最后插入的行在顶部 [英] how to get random rows in linq with last inserted row on top

查看:87
本文介绍了如何在linq中获得随机行,最后插入的行在顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对每个请求显示随机产品, OrderBy(r => Guid.NewGuid())效果很好,但是我尝试在表记录很大时提高性能,因此我在此处

i am trying to show random products in view for each request, this OrderBy(r => Guid.NewGuid()) works fine, but i am trying to increase perfomance when table records are huge , so i used second option from here

我的动作

public ActionResult ProductType(string id)
{
List<ProductsView> productlist = (from a in this.dbo.ProductTable
                                 join ca in dbo.Category on a.CategoryID equals ca.CategoryID
                                 where ca.Category == id 
                                 select new ProductsView()
                                 {
                                 CategoryID = c.CategoryID,
                                 Categorycount = c.Categorycount
                                 }).ToList<ProductsView>();

// here shuffle or mix products

int count = productlist.Count();
int index = new Random().Next(count);
ViewBag.Products = productlist.Skip(index).ToList();

 return View();

}

但是当视图返回时,缺少一些记录,例如:

but when view returns some records are missing, eg:

第一个请求 数4 索引1

在视图中显示3个产品

第二次请求 数4 索引2

显示2个产品在视图中

第三个请求 数4 索引3

显示1个产品在视图中

最后我还有一个要求,我可以在顶部显示最后插入的行并让其他产品随机吗?

finally i have one more requirement can i show last inserted row in top and let other products be random ?

我可以知道我在想什么吗?

may i know what i am missing ?

任何帮助都会很棒.

推荐答案

好的,所以更新的要求是:

Okay, so the updated requirements are:

  • 获取所有项目
  • 除了第一个项目(它应该是最后一个添加的项目)之外,订购是随机的

首先,摆脱您的Skip通话.您没有在尝试跳过任何内容.只需将所有产品(可能已订购,请参见下文)放入列表中即可.

Firstly, get rid of your Skip call. You're not trying to skip anything. Just fetch all the products (possibly ordered - see below) into a list.

对于随机性部分,我会在调用代码中使用经过修改的Fischer-Yates随机播放来完成-在Stack Overflow上有很多示例,例如

For the randomness part, I'd do that in the calling code, using a modified Fischer-Yates shuffle - there are numerous examples of that on Stack Overflow, such as here.

在这种情况下,您可能希望将最新的项目移到列表的开头,然后将列表的 rest 随机排序.只需对改组的代码稍作修改,就很容易了-但是您需要首先将最新的项目移到列表的开头.您可以通过在LINQ查询中使用OrderByDescending(x => x.InsertionDate)(或其他任何方法)来执行此操作,或者仅获取所有内容并在内存行中的O(n)中找到最新行.使用OrderByDescending会更简单,但效率可能会稍低(因为您实际上并不需要完全订购).

In this case, you probably want to get the most recent item to the start of the list and then just shuffle the rest of the list. That's easy enough to do with a slight modification to the shuffling code - but you need to get the most recent item to the start of the list first. You could either do that by using OrderByDescending(x => x.InsertionDate) (or whatever) in your LINQ query, or just fetch everything and find the latest row in an O(n) pass over the rows in memory. Using OrderByDescending will be simpler, but potentially slightly less efficient (as you don't really need full ordering).

这篇关于如何在linq中获得随机行,最后插入的行在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆