C#/EF获取随机数据
- /// <summary>
- /// 获取随机推荐的资源
- /// </summary>
- public List<ResourceOutputDto> GetRandomRecommendResources(int count)
- {
- var result = new List<ResourceOutputDto>();
- Random rd = new Random();
- var maxCount = _resourceManager.GetAll().Where(f => f.Status == true).Count();
- for (int i = 0; i < count; i++)
- {
- var randomCount = rd.Next(1, maxCount);
- var model = _resourceManager.GetAll().Where(f => f.Status == true).OrderBy(f => f.Id).Skip(randomCount - 1).Take(1).FirstOrDefault();
- if (model != null)
- {
- result.Add(model.MapTo<ResourceOutputDto>());
- }
- }
- return result;
- }