2014-03-19 90 views
0

我有4列,facilities0id,名稱,日期,數量。我在這裏幾乎沒有問題使用orderby使用linq查詢

如何使用orderby根據數量。我想找到前三排。任何人都可以幫助,謝謝

DataClassesDataContext db = new DataClassesDataContext(); 

    var query = from p in db.Facilities 
       join v in db.Reservations on p.facilities_id equals v.facilities_id 
       join c in db.Reservation_Details on v.reservation_id equals  
       c.resevation_id 
       where SqlMethods.Like(c.date, "%" + DropDownList1.Text + "%") 
       select new 
       { 
        p.facilities_id, 
        p.name, 
        c.date, 
        Quantity = p.Reservations.Count() 

       }; 

    GridView1.DataSource = query.GroupBy(x => x.facilities_id) 
         .Select(g => g.First()) 
         .ToList(); 

    GridView1.DataBind(); 

    int totalRowsCount = GridView1.Rows.Count; 
    Label2.Text = DateTime.Now.ToShortDateString(); 
    Label3.Text = totalRowsCount.ToString() + " record(s)"; 
    Label4.Text = DropDownList1.Text; 

數據在我的數據庫

facilities_id  name  date  Quantity 
    F001    cc  12-12-2014  3 
    F002    vv  12-12-2014  2 
    F003    gg  12-12-2014  1 
    F004    bb  12-12-2014  5 

這是我的期望輸出

facilities_id  name  date  Quantity 
    F004    bb  12-12-2014  5 
    F001    cc  12-12-2014  3 
    F002    vv  12-12-2014  2 

回答

4

使用OrderByDescending降序排列Take首先Quantity你的記錄進行排序,然後3記錄:

query.GroupBy(x => x.facilities_id) 
    .Select(g => g.First()) 
    .OrderByDescending(x => x.Quantity) 
    .Take(3) 
    .ToList(); 
1

使用降序您可以通過降列數量需要首先使用順序,然後採取先 「OrderByDescending」

.OrderByDescending(x => x.Quantity);

使用對於升序 「排序依據」

OrderBy(x => x.Quantity);

0

三排