views:

873

answers:

2

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

here is the answer in C#: http://stackoverflow.com/questions/470440/linq-how-to-select-only-the-records-with-the-highest-date

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/

+3  A: 
Dim q = From n In table _
        Group n By n.AccountId Into g _
        Select g.OrderByDescending(Function(t) t.Date).First()
Joel Coehoorn
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
this code does not work. "g" is not an acceptable "groupname"
Watki02
Forgot the underscores...
Joel Coehoorn
@Joel: There's still a problem with the group Into g which doesn't work in VB.
Meta-Knight
I think it might just work by replacing g with Group.
Meta-Knight
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
Did you try it with Group? You could also try "Group n By n.AccountId Into g = Group"...
Meta-Knight
+1  A: 

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
yeah, I think that was it! blasted vb syntax... thanks!
Watki02