简体   繁体   English

如何计算datagridview中的重复项

[英]How to count repeated items in datagridview

I have datagridview1 column filled with data: Room1, Room2, Room3, Room1, Room3, and also some data in row so it's like this: 我在datagridview1列中填充了数据:Room1,Room2,Room3,Room1,Room3,以及行中的一些数据,因此如下所示:

Name, Time, Room
name1, 10,  Room1
name2, 44,  Room2
name3, 1,   Room3
name4, 12,  Room1
.................

What i'd like to to is to count number or repeats - 2x Room1, 1x Room2, 2x Room3 and add cumulative time values. 我想要计数或重复-2x Room1、1x Room2、2x Room3并添加累积时间值。

So it should look like: 所以它应该看起来像:

Room    |  Cumulative_Time
-------------------------
Room1   |  22 
Room2   |  44

And add it to another datagridview.. Any ideas how to do it? 并将其添加到另一个datagridview。任何想法如何做到这一点?

Here you go with the lambda and linq flavours: 在这里,您可以品尝到lambda和linq的风味:

public List<Room> GetRooms()
{
    return new List<Room>(){new Room(){Name = "Room1", Time= 10},
                            new Room(){Name = "Room1", Time= 20},
                            new Room(){Name = "Room2", Time= 10},
                            new Room(){Name = "Room2", Time= 30},
                            new Room(){Name = "Room2", Time= 50},
                            new Room(){Name = "Room4", Time= 25},
                            new Room(){Name = "Room3", Time= 50},
                            new Room(){Name = "Room3", Time= 15},
                            new Room(){Name = "Room3", Time= 30},
                            new Room(){Name = "Room3", Time= 40}};
}

[TestMethod]
public void CheckSumsLambda()
{
    var rooms = GetRooms();
    var results = rooms.GroupBy(x => x.Name).Select(x => new { RoomName = x.Key, Occurences = x.Count(), Cumulativetime = x.Sum(y => y.Time) });

    Console.WriteLine("Lambda flavour");
    foreach (var result in results)
    {                 
        Console.WriteLine("{0}x {1}  | {2}", result.Occurences, result.RoomName, result.Cumulativetime);               
    }

}

[TestMethod]
public void CheckSumsLinq()
{
    var rooms = GetRooms();
    var results = from r in rooms
                    group r by r.Name
                        into g
                        select new { RoomName = g.Key, Occurences = g.Count(), Cumulativetime = g.Sum(y => y.Time) };

    Console.WriteLine("Linq flavour");
    foreach (var result in results)
    {
        Console.WriteLine("{0}x {1}  | {2}", result.Occurences, result.RoomName, result.Cumulativetime);
    }

}

The output: 输出:

lambda版本

linq版本

If your data comes from a query, you could populate your second grid with a query that uses a sum function on Time, grouped by the Room field 如果您的数据来自查询,则可以在查询中填充第二个网格,该查询使用“时间”上的求和函数,并按“房间”字段分组

SELECT Room, Sum(Time) from data_table group by Room

If you don't want/can't use a SQL query, you could do it using Linq, but I can't help you with the syntax... 如果您不想/不能使用SQL查询,可以使用Linq,但是我不能帮您语法...

Assuming you use POCOs as your DataSource, this should solve your problem. 假设您使用POCO作为数据源,这应该可以解决您的问题。

// Get data from DataGridView
var dts = DataGridView.DataSource;

// Get list of rooms
var rooms = dts.Select(d=>d.Room).Distinct();

// Populate new DataGrid
NewDataGridView.DataSource = rooms
.Select(r => new {Room = r, 
Cumulative_Time = dts.Where(d => d.room == r).Select(d => d.time).Sum()});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM