I have this problem, I have a shopping table
Purchases(Date DateTime, Number string)
I want to create a new record, so I need Max (Number), the problem is that Number is a string , I tried
Purchases.Select(X=>int.Parse(X.Number)).Max()
but this may throw an exception, I create a custom ToInt() extension, so when I use
Purchases.Select(X=>X.Number.ToInt()).Max()
it throws an exception saying that my ToInt () cannot be used with linq request in the same way as the famous ToString ()
so my question is : is there a way to pass a string to int in a linq query and handle the exceptions at the same time or integrate user-defined functions into a linq query !!
and this is my extension
public static int ToInt(this string s) { try { return int.Parse(s); } catch { } return 0; }
source share