如何在 linq where 中使用 TryParse
How to use TryParse in linq where
else{
if(a.Item2 == ">"){
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && double.TryParse(y.Value,out double res) == double.TryParse(a.Item3,out double res1)));
//result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) > Convert.ToDouble(a.Item3)));
}
else if(a.Item2 == "<")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) < Convert.ToDouble(a.Item3)));
else if(a.Item2 == "<=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) <= Convert.ToDouble(a.Item3)));
else if(a.Item2 == ">=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) >= Convert.ToDouble(a.Item3)));
else if(a.Item2 == "==")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) == Convert.ToDouble(a.Item3)));
else if(a.Item2 == "!=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) != Convert.ToDouble(a.Item3)));
else
throw new Exception("Comparison operator not found");
}
如何在上述代码情况下使用 double.TryParse
代替 Convert.ToDouble
,因为我不知道如何使用输出值进行比较。我想使用 TryParse
,因为我收到以下错误消息:
input string was not in a correct format
我如何在当前的情况下实现这一点?
有点 long-winded,但假设您只想 return 可以将值解析为双精度的行:
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1
&& double.TryParse(y.Value,out double res) // Value can be parsed
&& double.TryParse(a.Item3,out double res1) // Item3 can be parsed
&& res == res1)); // the 2 parsed results match
else{
if(a.Item2 == ">"){
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && double.TryParse(y.Value,out double res) == double.TryParse(a.Item3,out double res1)));
//result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) > Convert.ToDouble(a.Item3)));
}
else if(a.Item2 == "<")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) < Convert.ToDouble(a.Item3)));
else if(a.Item2 == "<=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) <= Convert.ToDouble(a.Item3)));
else if(a.Item2 == ">=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) >= Convert.ToDouble(a.Item3)));
else if(a.Item2 == "==")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) == Convert.ToDouble(a.Item3)));
else if(a.Item2 == "!=")
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1 && Convert.ToDouble(y.Value) != Convert.ToDouble(a.Item3)));
else
throw new Exception("Comparison operator not found");
}
如何在上述代码情况下使用 double.TryParse
代替 Convert.ToDouble
,因为我不知道如何使用输出值进行比较。我想使用 TryParse
,因为我收到以下错误消息:
input string was not in a correct format
我如何在当前的情况下实现这一点?
有点 long-winded,但假设您只想 return 可以将值解析为双精度的行:
result = dict.Where(x => x.Value.Any(y => y.Key == a.Item1
&& double.TryParse(y.Value,out double res) // Value can be parsed
&& double.TryParse(a.Item3,out double res1) // Item3 can be parsed
&& res == res1)); // the 2 parsed results match