根据时间间隔查询linq

时间:2018-01-08 20:03:56

标签: c# sql asp.net-mvc linq

我检索了数据,但我的数据来自每一分钟,但是,我每隔5分钟就会返回一次数据

例如:

MyCurruntData:

[12:00, 12:02, 12:03, 12:04, 12:05, 12:06, 12:07, 12:08, 12:09, 12:10, 12:11, 12:12, 12:13, 12:14, 12:15, 12:16, 12:17, 12:18, 12:19, 12:20]

myGol是:[12:00,12:05,12:10,12:20,]

我的linq:

    public ActionResult GetData()
    {

        FireViewModel _fireViewModel = new FireViewModel();
        var query = (from w1 in db.V_PDCPowerDemandArchiveForMis
            where db.V_LastEafHeat.Any(w2 => w1.DATE >= w2.StartTime)
            orderby w1.DATE descending
            select w1).Take(20).ToList();

        foreach (var item in query)
        {
            _fireViewModel.Power.Add(item.EAF1);
            _fireViewModel.Date.Add(item.DATE.ToShortTimeString());
        }

        return PartialView("_Fire",_fireViewModel);
    }

先谢谢

2 个答案:

答案 0 :(得分:0)

我假设您要实现的目的是对记录进行分组,以便您可以找到属于12:00到12:05范围的记录,属于12:05到12:10范围的记录,等。

然后你需要将时间的一小部分替换为00,05,10等。 你可以通过做一轮来做到这一点。将第二部分转换为浮点数,除以5:02变为0.4,08变为1.6等。然后用ROUND(n,0)舍入到下一个整数,0.2变为0而0.8变为2.一旦完成,则乘以到5,你得到的数字从5跳到5.见下面的例子

DECLARE @Nu TABLE (valsec CHAR(2), valfloat FLOAT)
INSERT INTO @Nu (valsec) VALUES ('01')
INSERT INTO @Nu (valsec) VALUES ('02')
INSERT INTO @Nu (valsec) VALUES ('03')
INSERT INTO @Nu (valsec) VALUES ('04')
INSERT INTO @Nu (valsec) VALUES ('05')
INSERT INTO @Nu (valsec) VALUES ('06')
INSERT INTO @Nu (valsec) VALUES ('07')
INSERT INTO @Nu (valsec) VALUES ('08')
INSERT INTO @Nu (valsec) VALUES ('09')
INSERT INTO @Nu (valsec) VALUES ('10')
INSERT INTO @Nu (valsec) VALUES ('11')
INSERT INTO @Nu (valsec) VALUES ('12')
INSERT INTO @Nu (valsec) VALUES ('13')
INSERT INTO @Nu (valsec) VALUES ('14')
INSERT INTO @Nu (valsec) VALUES ('15')
INSERT INTO @Nu (valsec) VALUES ('16')
INSERT INTO @Nu (valsec) VALUES ('17')
INSERT INTO @Nu (valsec) VALUES ('18')
INSERT INTO @Nu (valsec) VALUES ('19')
INSERT INTO @Nu (valsec) VALUES ('20')
UPDATE @Nu SET valfloat = CAST (valsec AS FLOAT) / 5
SELECT 
    valsec,
    valfloat,
    ROUND (valfloat,0)*5 AS secint,
    RIGHT (CAST (( ROUND (valfloat,0)*5 + 100) AS VARCHAR(3) ),2) AS secchar
FROM @Nu 

结果:

01  0.2 0   00
02  0.4 0   00
03  0.6 5   05
04  0.8 5   05
05  1   5   05
06  1.2 5   05
07  1.4 5   05
08  1.6 10  10
09  1.8 10  10
10  2   10  10
11  2.2 10  10
12  2.4 10  10
13  2.6 15  15
14  2.8 15  15
15  3   15  15
16  3.2 15  15
17  3.4 15  15
18  3.6 20  20
19  3.8 20  20
20  4   20  20

注意:

部分
RIGHT (CAST (( ROUND (valfloat,0)*5 + 100) AS VARCHAR(3) ),2) 

只能通过添加100并将最后两个字符(因此得到05而不是5)将数字转换为char(2)

答案 1 :(得分:0)

关键是w1.Minute%5 == 0我应该在foreach中添加一个条件:

 public ActionResult GetFuranceOne()
    {
        FireViewModel _fireViewModel  = new FireViewModel();

        var query = (from w1 in db.V_PDCPowerDemandArchiveForMis
                     where db.V_LastEafHeat.Any(w2 => w1.DATE >= w2.StartTime)
                     orderby w1.DATE descending
                     select w1).Take(50);


        foreach (var item in query.ToList())
        {
            if (item.DATE.Minute % 5==0)
            {
                _fireViewModel.Power.Add(item.EAF1);
                _fireViewModel.Date.Add(item.DATE.ToShortTimeString());
            }
        }
        return PartialView("_Fire", _fireViewModel);
    }