问题
I have four column, facilities_id, name, date and quantity for my gridview. My problem is it have same facilities_id in my result. So how can i settle it, anyone can help me? Thanks..
In additional, i hope can find the top 2 row in gridview. Mean just show up the top 2 row.. 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;
GridView1.DataBind();
Here is my output, how to remove the same facilities_id.
facilities_id name date quantity
F001 aa 12/01/2014 1
F001 aa 12/01/2014 12
F002 bb 12/01/2014 1
F003 cc 12/01/2014 1
i hope can become like this.
facilities_id name date quantity
F001 aa 12/01/2014 13
F002 bb 12/01/2014 1
F003 cc 12/01/2014 1
回答1:
GridView1.DataSource = query.DistinctBy( i => i.facilities_id);
or
You can use GroupBy() and select the first item of each group to achieve what you want - assuming you want to pick one item for each distinct facilities_idproperty:
GridView1.DataSource = query.GroupBy(x => x.facilities_id)
.Select(g => g.First())
.ToList();
回答2:
This is the code for unique facilities_id. I dont know that you are getting data from dataset or table.
I have return a code which is retrieving the code in dataset and that dataset going to be unique.
And also your quantity will be added but facilities_id will not be repeated.
var sells = from row in ds.Tables["cartTable"].AsEnumerable() ("Write here your table name as I dont know your table name ")
group row by row.Field<string>("facilities_id") into rowGroup
select new
{
facilities_id = rowGroup.Key,
name = string.Join(" ", rowGroup.Select(row => row.Field<string>("name")).Distinct().ToArray()),
date = string.Join(" ", rowGroup.Select(row => row.Field<string>("date")).Distinct().ToArray()),
price = string.Join(" ", rowGroup.Select(row => row.Field<decimal>("price")).Distinct().ToArray()),
Quantity = rowGroup.Sum(r => r.Field<int>("quantity")),
};
I hope this code will be useful to you......!!!!
来源:https://stackoverflow.com/questions/22454013/remove-duplication-row-in-gridview