1
        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");          
        }

How can i use double.TryParse in the above code situation inplace of Convert.ToDouble as i dont know how to use the out value for the comparison. I want to use TryParse because I'm getting the following error message :

input string was not in a correct format

How i can achive this in the current senario?

nalka
  • 1,894
  • 11
  • 26
Talha Kazi
  • 55
  • 4
  • 1
    What should be the result when `TryParse` returns `false`? – ProgrammingLlama Oct 02 '20 at 07:09
  • 1
    [This might be what you want](https://stackoverflow.com/questions/16613224/can-i-use-a-tryparse-inside-linq-comparable). – ProgrammingLlama Oct 02 '20 at 07:10
  • 1
    The `out` variables are available for use after the corresponding method was called. So just add a `&& res == res1` at the end (Keep in mind though you have to fix more in the query, for example you use the assingment operator `=` instead of the equality comparison `==`. Though you probably want to use the `&&` operator instead to verify that both parses succeeded). Also `TryParse` will not fix incorrect input strings but instead of throwing an exception it will return `false` and set the `out` parameter to it's default value. –  Oct 02 '20 at 07:14
  • 4
    So it'll probably look something like this (actual implementation might be different since it's not completely clear what functionality you're looking for): `y => y.Key == a.Item1 && double.TryParse(y.Value, out double res) && double.TryParse(a.Item3, out double res1) && res == res1` –  Oct 02 '20 at 07:16
  • Thank you @Knoop That worked fine in my case and the error is gone – Talha Kazi Oct 02 '20 at 08:29

1 Answers1

3

It gets a little long-winded, but assuming you want to only return the rows where the values can be parsed to a double:

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
Jamiec
  • 133,658
  • 13
  • 134
  • 193