lekso
lekso

Reputation: 1813

LINQ query grouping

I have a problem with LINQ query. Could somebody please help?

There is one table Content with Columns Id (unique), ContentId, Version. I'd like to get the latest version for each unique Content Id.

So if table is:

 - Id, ContentId, Version
 - 1, 1, 1
 - 2, 1, 2
 - 3, 2, 1

Then query should return:

 - Id, ContentId, Version
 - 2, 1, 2
 - 3, 2, 1

Upvotes: 0

Views: 42

Answers (1)

Arion
Arion

Reputation: 31239

Maybe something like this:

var result= (
        from c in db.Content
        where db.Content
                  .Where (l =>l.ContentId==c.ContentId)
                  .Max (l=>l.Version)==c.Version
        select c
    );

Where db is the linq data context

Upvotes: 1

Related Questions