我已经可以组合2个相同的facilities_id,但我想数量也结合在一起。怎么解决?
如何根据数量查找gridview中的前2行?谢谢
facilities_id name date quantity
F001 aa 12Jan2014 1
F001 aa 12Jan2014 12
F002 bb 14Jan2014 1
F003 cc 15Jan2014 1
我希望输出可以变成这样。
facilities_id name date quantity
F001 aa 12Jan2014 13
F002 bb 14Jan2014 1
F003 cc 15Jan2014 1
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();
答案 0 :(得分:0)
即使在绑定到网格之前,你不应该这样做吗?看起来你想要按前3列进行分组,并希望最后得到一笔金额。那么为什么不在将数据返回到UI级别之前执行此操作呢?无论如何,如果你想在你的代码中做groupby,它应该是这样的。我从未尝试将匿名绑定到网格,因此可能需要强类型对象而不是匿名对象。
GridView1.DataSource = query.GroupBy(x => x.facilities_id)
.Select(g => new
{g.Key, g.First().name,
g.First().date, g.Sum(gg=>gg.Quantity)})
.ToList();
GridView1.DataBind();