|
@@ -0,0 +1,65 @@
|
|
1
|
+一种会报错的写法
|
|
2
|
+
|
|
3
|
+```
|
|
4
|
+var macBreakerDatas = _breakerDataRepo.GetAll().Where(t => t.mac == mac.Mac);
|
|
5
|
+
|
|
6
|
+var removedBreakerDatas = macBreakerDatas.Where(t => (DateTime.Now - DateTime.Parse(t.CreateTime)).Minutes >= 65);
|
|
7
|
+
|
|
8
|
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
|
|
9
|
+```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+不报错,但查询效率慢
|
|
14
|
+
|
|
15
|
+```
|
|
16
|
+var macBreakerDatas = _breakerDataRepo.GetAll().Where(t => t.mac == mac.Mac);
|
|
17
|
+
|
|
18
|
+var removedBreakerDatas = macBreakerDatas.ToList().Where(t => (DateTime.Now - DateTime.Parse(t.CreateTime)).Minutes >= 65);
|
|
19
|
+
|
|
20
|
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
|
|
21
|
+```
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+修改基础设施层
|
|
26
|
+
|
|
27
|
+```
|
|
28
|
+ public interface IOpenBreakerDataRepository : IRepository<BreakerData, Guid>
|
|
29
|
+ {
|
|
30
|
+ IQueryable<BreakerData> GetAll();
|
|
31
|
+ IQueryable<BreakerData> GetAllBySql(string sql);
|
|
32
|
+ }
|
|
33
|
+ public class OpenBreakerDataRepository : Repository<BreakerData, Guid, OpenAPIContext>, IOpenBreakerDataRepository
|
|
34
|
+ {
|
|
35
|
+ public OpenBreakerDataRepository(OpenAPIContext context) : base(context)
|
|
36
|
+ {
|
|
37
|
+
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public IQueryable<BreakerData> GetAll()
|
|
41
|
+ {
|
|
42
|
+ return DbContext.BreakerDatas.Where(t => true);
|
|
43
|
+
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public IQueryable<BreakerData> GetAllBySql(string sql)
|
|
47
|
+ {
|
|
48
|
+ return DbContext.BreakerDatas.FromSqlRaw(sql);
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+```
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+最后改成
|
|
56
|
+
|
|
57
|
+```
|
|
58
|
+string sixtyFiveAgoTimeStr = DateTime.Now.AddMinutes(-65).ToFullTimeStr();
|
|
59
|
+
|
|
60
|
+string rawSql = $"select * from breakerdatas where mac='{mac.Mac}' and str_to_date(CreateTime, '%Y-%m-%d %H:%i:%s') <= str_to_date('{sixtyFiveAgoTimeStr}', '%Y-%m-%d %H:%i:%s')";
|
|
61
|
+
|
|
62
|
+var removedBreakerDatas = _breakerDataRepo.GetAllBySql(rawSql);
|
|
63
|
+_breakerDataRepo.RemoveRange(removedBreakerDatas);
|
|
64
|
+```
|
|
65
|
+
|