3

I know how to do this in C#, but my dev team doesn't use C#...

here is the answer in C#: How to select only the records with the highest date in LINQ

How do I do this in VB?

Essentially, if I knew how to write lambda expressions in VB, I would be set, but the materials I have found are not helpful.

I also need to know why the Into identifier (i.e. "g") is always trying to be a function every time I move off the line, resulting in this error:

http://img19.imageshack.us/i/errno.png/

Community
  • 1
  • 1
Watki02
  • 4,696
  • 7
  • 34
  • 36

2 Answers2

8
Dim q = From n In table _
        Group n By n.AccountId Into g _
        Select g.OrderByDescending(Function(t) t.Date).First() 
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • as I put that into my code, the pre-compiler (I think) keeps turning "g" into "g()" as soon as I move off the line, thus throwing an error. It thinks g needs be a function ?? – Watki02 Oct 05 '09 at 17:23
  • this code does not work. "g" is not an acceptable "groupname" – Watki02 Oct 05 '09 at 17:39
  • @Joel: There's still a problem with the group Into g which doesn't work in VB. – Meta-Knight Oct 05 '09 at 18:46
  • That actually just confuses the compiler. "Group" is a key word. The environment keeps trying to add "()" after "g", like it is supposed to be a function. I am wondering if that is because this code is in an event handler in a class (don't know why that would make a difference), or if it is because I might have an outdated version of the framework? All I know is that it doesn't work as is. – Watki02 Oct 06 '09 at 20:45
  • 1
    Did you try it with Group? You could also try "Group n By n.AccountId Into g = Group"... – Meta-Knight Oct 07 '09 at 21:20
8

Here's an example from MSDN of grouping in VB:

Dim query = From p In db.Products _
            Group p By p.CategoryID Into g = Group _
            Select CategoryID, MaxPrice = g.Max(Function(p) p.UnitPrice)

If you omit the "= Group", it will consider g as a function. Hope this helps.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58