Accepted answer

Your max function is trying to return an Int64. The overload of Max you are using is defined here on MSDN. You'll see that it takes a parameter Func<TSource, long> selector. This is your lambda expression and says that you'll give it a TSource and it will return a long (ie Int64). What your function is doing is when x.Value is null returning a null value which is of course not valid to as an Int64.

In this case you should be able to use something like:

x=> x.Value!=null?x.Value:Int64.Minvalue

This means that if x has a value it returns that and otherwise returns the minimum possible value which will only ever be the max if everything is null.

I should disclaim that this code is untested but should give you the right idea.

edited: I originally was assuming that this was a nullable long but it turns out there are overloads that support that so I now assume that x is not a nullable long, just looks a bit like one if you squint int eh right way.

Related Articles