Search code examples
linqexpressionaggregateunhandledmethod-invocation

Linq Aggregate produces error: "Exception has been thrown by the target of an invocation. // Unhandled Expression Type: 1001"


The following line of code keeps crashing on me with: "Exception has been thrown by the target of an invocation. // Unhandled Expression Type: 1001"

Can anyone tell why just by looking at it?

myList.Aggregate((curmax, x) => (curmax == null || x.LastMonth > curmax.LastMonth ? x : curmax));

What i do get is that curmax is the variable that will be returned and that X is an item being iterated over. but i just can not explain the error.


Solution

  • I suspect this is just an operation which is unsupported by NHibernate. However, if you're just after "the value with the largest value for LastMonth" you could use:

    var latestPeriod = myList.OrderByDescending(x => x.LastMonth)
                             .FirstOrDefault();
    

    That would be considerably simpler, and more likely to be supported.