删除gridview中的重复行

时间:2014-03-17 12:14:31

标签: c# asp.net gridview

我的facilities_id有四列,namedatequantitygridview。我的问题是我的结果中有facilities_id。那我怎么解决它,谁都可以帮助我?谢谢..

另外,我希望能找到gridview中的前两行。意思是只显示前2行.. 感谢

    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();

这是我的输出,如何删除相同的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  

我希望能成为这样的人。

facilities_id  name    date        quantity
  F001           aa    12/01/2014      13
  F002           bb    12/01/2014      1
  F003           cc    12/01/2014      1  

2 个答案:

答案 0 :(得分:0)

GridView1.DataSource = query.DistinctBy( i => i.facilities_id);

你可以使用GroupBy()并选择每个组的第一项来实现你想要的 - 假设你想为每个不同的facilities_idproperty挑选一个项目:

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

答案 1 :(得分:0)

这是唯一的facility_id的代码。 我不知道你是从数据集或表中获取数据。

我已经返回了一个代码,该代码检索数据集中的代码,该数据集将是唯一的。

此外,您的数量也会被添加,但不会重复来自facility_id。

     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......!!!!