我想得到一个表中所有元素的列表,它们具有相同的序列号,但我不知道序列号。它必须是表格中所选行的最后一个序列号。
SequenceNo LineId SubId DataCol1 DataCol2 DataCol3
33 1 1 35 36 37
33 1 2 25 26 27
33 1 3 45 46 47
34 1 1 23 42 32
34 1 2 42 11 12
34 1 3 23 33 32
所以到目前为止我的代码是:
Context.NextLineSequeceData.Where(c => c.LineId == LineId).OrderByDescending(c=>c.SequenceNo).ToList();
如何修改它以便我只获取SequenceNo 34的数据?
答案 0 :(得分:1)
说到Linq,我更喜欢简单易读的语句,所以这里有一个简单的解决方案:
var maxSequence = Context.NextLineSequeceData
.Where(x => x.LineId == selectedLineId)
.Select(x => x.SequenceNo)
.Max();
var result = Context.NextLineSequeceData.Where(x => x.SequenceNo == maxSequence);
答案 1 :(得分:0)
如果你想获得上面示例的最后一行 - 这是一个如何在SQL服务器上实现此目的的示例:
declare @t table(i int, ch char(5))
insert into @t
select 1 ,'aa'
union all select 5 ,'bb'
union all select 6 ,'vv'
union all select 2,'sd'
union all select 2,'sf'
union all select 1,'sdf'
union all select 5,'sf'
select * from @t
;with cte as (
select rn=row_number() over(order by i),i,ch
from @t)
select *
from cte
;with cte as (
select rn=row_number() over(order by i),i,ch
from @t)
select *
from cte c
where c.rn=(select max(rn) from cte)