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