使用Entity Framework或GridView在C#ASP.Net中透视数据?

时间:2017-07-10 06:33:02

标签: c# asp.net entity-framework gridview

我基本上有这个数据集:

MyDbContext db = new MyDbContext();
GridView gridView = new GridView();
gridView.DataSource = db.Services;
gridView.DataBind();

这给了我这张桌子:

+-----------+-----------+---------+---------+
|   Name    |  Machine  | Status  | Version |
+-----------+-----------+---------+---------+
| Service 1 | Machine 1 | Running |   2.2.2 |
| Service 1 | Machine 2 | Running |   2.2.2 |
| Service 1 | Machine 3 | Running |   2.2.2 |
| Service 2 | Machine 1 | Running |   2.2.2 |
| Service 2 | Machine 2 | Running |   2.2.2 |
| Service 2 | Machine 3 | Running |   2.2.2 |
| Service 3 | Machine 1 | Running |   2.2.2 |
| Service 3 | Machine 2 | Running |   2.2.2 |
| Service 3 | Machine 3 | Running |   2.2.2 |
+-----------+-----------+---------+---------+

我现在希望" pivot" name列来获取这样的表:

+-----------+-----------+-----------+-----------+
|  Machine  | Service 1 | Service 2 | Service 3 |
+-----------+-----------+-----------+-----------+
| Machine 1 | Running   | Running   | Running   |
| Machine 2 | Running   | Running   | Running   |
| Machine 3 | Running   | Running   | Running   |
+-----------+-----------+-----------+-----------+

我只是想知道Entity Framework或GridView是否有任何好的工具来执行此操作。我一直试图在谷歌上找到一个解决方案,但由于没有创建我自己的枢轴功能,我还没有找到任何有用的解决方案。

编辑2:

注意:这是我实际环境的精简版。我有超过3台机器需要监控,并且能够动态添加更多机器。我目前只有3个服务,但这可能会在现有机器或新机器上以Service 4Service 5进行扩展。所以我需要一个通用的旋转解决方案。谢谢!

修改1:

我尝试使用实体框架通过使用PIVOT创建视图:

db.Database.ExecuteSqlCommand(
"CREATE VIEW pivotedServices AS (" +
"SELECT " +
    "Machine, [Service 1], [Service 2], [Service 3] " +
"FROM " +
    "(dbo.Services) " +
"PIVOT" +
"( Status FOR [Name] IN ( [Service 1], [Service 2], [Service 3])) AS pvt);"
);

但没有让它发挥作用。保持收到错误"('。",经过一些测试后,似乎是SELECT语句之前的第一个括号是问题。删除周围的括号在SELECT附近发出错误""。因此,我放弃了这个想法。

我也尝试过使用几个for循环来实现" pivot"手动,我得到了相当好的工作。但我觉得应该有更好的解决方案。如果没有,我猜是好的,但我希望你们有一个。

1 个答案:

答案 0 :(得分:2)

解决方案是:

首先,将您的表规范化为三个表('Services','Machines','Service_Machine'):

CREATE TABLE Services 
(
    ServiceId INTEGER PRIMARY KEY NOT NULL, 
    Name VARCHAR (25), 
    Status VARCHAR (25), 
    Version VARCHAR (25)
);

CREATE TABLE Machines 
(
    MachineId INTEGER PRIMARY KEY NOT NULL, 
    Name VARCHAR (25)
);

// this table is created as above two tables have Many-to-Many Relationship

CREATE TABLE Service_Machine 
(
    ServiceId INTEGER REFERENCES Services (ServiceId), 
    MachineId INTEGER REFERENCES Machines (MachineId)
);

然后创建两个视图('SimpleView','PivotView'):

CREATE VIEW SimpleView AS 
SELECT s.ServiceId, s.Name AS 'Name',m.MachineId, m.Name AS 'Machine',s.Status,s.Version 
FROM Services AS s,Machines AS m,Service_Machine AS sm 
WHERE s.ServiceId=sm.ServiceId 
AND m.MachineId=sm.MachineId

SimpleView输出,您可以使用gridview绑定:

enter image description here

<强>扩展

再创建一个视图ViewStatus

select distinct(sv.ServiceId) as 'ServiceId', sv.Name, sv.Status 
from SimpleView sv 
inner join SimpleView sv2 on sv.MachineId = sv2.MachineId 
inner join SimpleView sv3 on sv.ServiceId = sv2.ServiceId

现在您必须在 .CS代码中创建PivotView视图:

private void CreatePivotView()
    {
        MyDbContext db = new MyDbContext();
        DataTable dt = db.ViewStatus; // get data from ViewStatus that is in your database
        int count = 1;
        String query = "CREATE VIEW IF NOT EXISTS PivotView AS SELECT DISTINCT(Machine) AS 'Machine', ";

        foreach (DataRow dr in dt.Rows)
        {
            query += "(SELECT status FROM StatusView WHERE ServiceId=" + dr["ServiceId"] + ") AS 'Service " + dr["ServiceId"] + "'";
            if (dt.Rows.Count != count)
            {
                query += ";";
            }
            else
            {
                query += " ";
            }
            count++;
        }

        query += "from SimpleView";

        db.Database.ExecuteSqlCommand(query);
    }
您可以使用gridview绑定的

PivotView输出: enter image description here

注意: SQLITE Studio用于设计上述数据库查询。