1

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.

HerbalMart
  • 1,669
  • 3
  • 27
  • 50

2 Answers2

1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

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));
Bala R
  • 107,317
  • 23
  • 199
  • 210
HerbalMart
  • 1,669
  • 3
  • 27
  • 50
  • 1
    Are you aware that that means you're now pulling down *everything* in `myList` from the database? Does the solution I proposed not work? (It's rather more readable aside from anything else, IMO.) – Jon Skeet Nov 10 '11 at 09:12
  • Thanks for your comment Jon. At the time i wasn't aware of this. Now... with a better knowledge of concrete classes vs IEnumerables i am. In our case pulling down everything wasn't that bad performance wise. – HerbalMart Mar 28 '12 at 09:18