简体   繁体   中英

How can I use over by partition in 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM