for (int i = dt.Rows.Count-1; i >=0 ; i--)
时间: 2024-06-05 10:13:12 浏览: 12
这段代码是在使用 C# 编程语言中访问 DataTable 的行数据时进行倒序遍历的代码。其中,dt 是一个 DataTable 对象,Rows 是 DataTable 的一个属性,表示 DataTable 中的行集合。Count 是行集合中元素的数量,即行数。在这段代码中,使用 for 循环从最后一行开始遍历,即从 Count-1 开始,每次递减 1,直到遍历到第一行,即第 0 行。这样可以实现对 DataTable 行数据的倒序遍历。
相关问题
for (int i = 0; i < dt.Rows.Count; i++)
这是一个 C# 中的 for 循环,用于遍历 DataTable 中的每一行数据。dt 是一个 DataTable 对象,它包含了多个 DataRow 对象,每个 DataRow 对象代表一行数据。dt.Rows.Count 表示 dt 中包含的行数,循环的条件是 i < dt.Rows.Count,即 i 从 0 开始递增,直到 i 等于 dt.Rows.Count 时循环结束。在循环体中,可以使用 dt.Rows[i] 获取第 i 行数据。
List<string> NotCheckstationinfo = stationinfo.Where(i => i.分类 == "应用气象观测站" || i.分类 == "应用气象观测站(便捷)").Select(it => it.台站号).ToList(); loginfo.Info($"获取{dt.ToString("yyyy-MM-dd HH:00")}区域气象站天元信息"); re = GetObservData(token, RegionalStationConfig, dt); List<string> NotChectkstationinfo = new List<string>(); //新增 re.rows = re.rows.Where(it => !NotChectkstationinfo.Contains(it.station_code)).ToArray(); re.total = re.rows.Count(); if (re.total != 0) { foreach (var item in re.rows) { Ty_Monitor monitor = new Ty_Monitor(); monitor.observtime = dt; monitor.stationumber = item.station_code; monitor.region = item.town_name; monitor.type = ObservType.区域站.ToString(); int hour = dt.Hour; int yushu = hour % 8; int group = hour / 8 + 1; string type = string.Empty; switch (group) { case 1: type = item.group1.Substring(yushu * 2, 2); break; case 2: type = item.group2.Substring(yushu * 2, 2); break; case 3: type = item.group3.Substring(yushu * 2, 2); break; } monitor.errortype = int.Parse(type); Monitors.Add(monitor); } }依旧会把应用气象站(便捷)视为异常站点是为什么
根据代码,可能是因为在获取数据之前,已经将应用气象站(便捷)的台站号添加到了NotCheckstationinfo列表中,但在筛选数据时,使用的是名为NotChectkstationinfo的空列表,所以筛选时并没有排除应用气象站(便捷)的台站数据。
解决方法是,将列表名统一改为NotCheckstationinfo,并且在获取数据之前,先清空该列表,以防之前的数据对结果造成影响。代码修改如下:
```
List<string> NotCheckstationinfo = stationinfo.Where(i => i.分类 == "应用气象观测站" || i.分类 == "应用气象观测站(便捷)").Select(it => it.台站号).ToList();
loginfo.Info($"获取{dt.ToString("yyyy-MM-dd HH:00")}区域气象站天元信息");
re = GetObservData(token, RegionalStationConfig, dt);
NotCheckstationinfo.Clear(); //清空列表
re.rows = re.rows.Where(it => !NotCheckstationinfo.Contains(it.station_code)).ToArray();
re.total = re.rows.Count();
if (re.total != 0)
{
foreach (var item in re.rows)
{
Ty_Monitor monitor = new Ty_Monitor();
monitor.observtime = dt;
monitor.stationumber = item.station_code;
monitor.region = item.town_name;
monitor.type = ObservType.区域站.ToString();
int hour = dt.Hour;
int yushu = hour % 8;
int group = hour / 8 + 1;
string type = string.Empty;
switch (group)
{
case 1: type = item.group1.Substring(yushu * 2, 2); break;
case 2: type = item.group2.Substring(yushu * 2, 2); break;
case 3: type = item.group3.Substring(yushu * 2, 2); break;
}
monitor.errortype = int.Parse(type);
Monitors.Add(monitor);
}
}
```
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)