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