码迷,mamicode.com
首页 > 编程语言 > 详细

C# List 随机排序

时间:2016-01-28 18:40:37      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

 

1、List随机排序方法

  public List<T> RandomSortList<T>(List<T> ListT)
  {
    Random random = new Random();
    List<T> newList = new List<T>();
    foreach (T item in ListT)
    {
      newList.Insert(random.Next(newList.Count + 1), item);
    }
    return newList;
  }

2、随机生成题目

  /// <summary>
  /// 随机生成题目
  /// </summary>
  /// <returns></returns>
  public List<Entities.V_Quelibrary> GetRandomQue()
  {
    List<string> names = new List<string>();
    List<string> values = new List<string>();
    string itemId =Config.QueType;
    string hql = "from V_Quelibrary where state=1 and Item_Id=:Item_Id";
    names.Add("Item_Id");
    values.Add(itemId);
    List<Entities.V_Quelibrary> quelibraryList = queService.GetListQuelibraryNoPage(hql, names, values);
    List<Entities.V_Quelibrary> queList = new List<Entities.V_Quelibrary>();

    //筛选分值为2的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 2).ToList()).Take(2).ToList());

    //筛选分值为5的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 5).ToList()).Take(2).ToList());

    //筛选分值为8的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 8).ToList()).Take(2).ToList());

    //筛选分值为12的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 12).ToList()).Take(2).ToList());

    //筛选分值为20的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 20).ToList()).Take(2).ToList());

    //获得的list再次随机排序
    queList = this.RandomSortList<Entities.V_Quelibrary>(queList);
    return queList;
  }

 

C# List 随机排序

标签:

原文地址:http://www.cnblogs.com/wyhBlog/p/5166886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!