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

StackOverflow https://stackoverflow.com/questions/7674105

سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

I solved it by calling .Tolist() from Nhiberate. From there on the aggregate extension method from Microsoft Linq is used (in stead of the Nhibernate version) and no more problem.

myList.ToList().Aggregate((curmax, x) => (curmax == null || x.LastMonth > curmax.LastMonth ? x : curmax));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top