string[] errorNum = { errorStations[0].stationumber }; string[] errorTR = { "58453", "58555" }; ;将errorNum重新赋值成去掉errorTR的数组
时间: 2024-02-14 15:17:33 浏览: 30
可以使用 LINQ 中的 Except 方法来实现去除 errorTR 数组元素的操作,代码如下:
```
string[] errorNum = errorStations.Select(s => s.stationumber).Except(errorTR).ToArray();
```
其中,`Select` 方法用于从 `errorStations` 集合中选择 `stationumber` 属性,`Except` 方法用于去除 `errorTR` 数组中的元素,`ToArray` 方法用于将结果转换为字符串数组。
相关问题
string[] errorNum = { errorStations[0].stationumber }; string[] errorTR = { "58453", "58555" }; string[] errorGJ = { "58553", "58550", "58556" }; bool TRY = errorNum.Intersect(errorTR).Count() == errorNum.Count();//土壤水分是否全部掉线 bool GJY = errorNum.Intersect(errorTR.Concat(errorGJ)).Count() == errorNum.Count();//国家站是否全部掉线 if (TRY) { sb.AppendLine($"监控时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},故障区域:柯桥、新昌土壤水分同时缺测,故障诊断:疑为全市性掉线,一般不为县局问题,请市局值班员进行排查"); string[] errorNum = errorStations.Select(s => s.stationumber).Except(errorTR).ToArray(); }报错修改
在 if 语句块中重新定义了一个名为 `errorNum` 的字符串数组,导致与之前定义的 `errorNum` 变量冲突,从而编译错误。可以将重新定义的 `errorNum` 改为另一个名称即可,例如:
```
string[] errorNum = { errorStations[0].stationumber };
string[] errorTR = { "58453", "58555" };
string[] errorGJ = { "58553", "58550", "58556" };
bool TRY = errorNum.Intersect(errorTR).Count() == errorNum.Count();
bool GJY = errorNum.Intersect(errorTR.Concat(errorGJ)).Count() == errorNum.Count();
if (TRY)
{
sb.AppendLine($"监控时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},故障区域:柯桥、新昌土壤水分同时缺测,故障诊断:疑为全市性掉线,一般不为县局问题,请市局值班员进行排查");
string[] errorNumNew = errorStations.Select(s => s.stationumber).Except(errorTR).ToArray();
// 使用 errorNumNew 进行后续操作
}
```
在 if 语句块中使用了 `errorNumNew` 数组进行后续操作。
string[] errorNum = { station.stationumber }; if (errorNum.Contains(58453) && errorNum.Contains(58555))如何修改能让判定比较的是string类型数组元素
如果你想让判断的是 `string` 类型数组元素,你需要将 58453 和 58555 也用字符串的形式表示。可以这样修改代码:
```
string[] errorNum = { station.stationumber };
if (errorNum.Contains("58453") && errorNum.Contains("58555"))
{
// do something
}
```
这样修改后,就可以正确地判断 `errorNum` 数组中是否包含字符串元素 "58453" 和 "58555"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)