Reputation: 1
I have 4 column, facilities0id, name, date, Quantity. I have few problem here
How to use orderby based on the quantity. I wan to find top 3 row. Anyone can help, thanks
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;
Data in my database
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
Here is my expected output
facilities_id name date Quantity
F004 bb 12-12-2014 5
F001 cc 12-12-2014 3
F002 vv 12-12-2014 2
Upvotes: 0
Views: 72
Reputation: 67
you need to first use order by descending for column quantity and then take first three rows
Upvotes: 0
Reputation: 527
Use For Descending "OrderByDescending"
.OrderByDescending(x => x.Quantity);
Use For Ascending "OrderBy"
OrderBy(x => x.Quantity);
Upvotes: 1
Reputation: 101731
Use OrderByDescending
to sort your records by Quantity
in descending order,then Take
first 3
records:
query.GroupBy(x => x.facilities_id)
.Select(g => g.First())
.OrderByDescending(x => x.Quantity)
.Take(3)
.ToList();
Upvotes: 4