程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# List 隨機排序

C# List 隨機排序

編輯:C#基礎知識

 

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;
  }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved