Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use over by partition in LINQ?

Tags:

c#

sql

linq

I'm confused about how to change this query to LINQ

  select 
     CONTENT
  from
     (    
        select 
           CONTENT,
           CAM_ID,
           max(CAM_ID) over (partition by DOCUMENT_ID) MAX_ID
        from    
           T_CAM_REVISION
        where 
           DOCUMENT_ID = '101'
     )
  where     
     CAM_ID = MAX_ID

so I can get a single value of content.

like image 311
Mr.Rendezvous Avatar asked Sep 30 '11 06:09

Mr.Rendezvous


1 Answers

There is no way to do max(...) over (...) in LINQ. Here is an equivalent query:

var maxCamID =
    context.T_CAM_REVISION
    .Where(rev => rev.DOCUMENT_ID == "101")
    .Max(rev => rev.CAM_ID);

var query =
    from rev in context.T_CAM_REVISION
    where rev.CAM_ID == maxCamID
    where rev.DOCUMENT_ID == "101"
    select rev.CONTENT;

If you want only a single result:

var result =
    context.T_CAM_REVISION
    .First(rev => rev.CAM_ID == maxCamID
               && rev.DOCUMENT_ID == "101")
    .CONTENT;
like image 102
Markus Jarderot Avatar answered Nov 01 '22 18:11

Markus Jarderot