ID Name Score Time
----------------------------------
1 test1 92 6
----------------------------------
2 test2 100 10
----------------------------------
3 test3 88 80
----------------------------------
4 test4 90 120
----------------------------------
5 test5 70 100
----------------------------------
6 test6 78 60
----------------------------------
如题,ID为主键,我现在有ID为1、3、4、5的一个集合。我想查询这个集合里面的Time的最大值(该例子应返回120,即ID为4的这条记录的Time值最大。但是我这个集合是动态的,有可能只为1、2两条记录,那么返回的值就应该是ID为2的那条Time=10)。请问大侠们用Linq to sql 该如何实现。或者直接sql也可以。
public class Test { public int ID { get; set; } public string Name { get; set; } public int Score { get; set; } public int Time { get; set; } } List<Test> tests = new List<Test>(); tests.Add(new Test() { ID = 1, Name = "test1", Score = 92, Time = 6 }); tests.Add(new Test() { ID = 2, Name = "test2", Score = 100, Time = 10 }); tests.Add(new Test() { ID = 3, Name = "test3", Score = 88, Time = 80 }); tests.Add(new Test() { ID = 4, Name = "test4", Score = 90, Time = 120 }); tests.Add(new Test() { ID = 5, Name = "test5", Score = 70, Time = 100 }); tests.Add(new Test() { ID = 6, Name = "test6", Score = 78, Time = 60 }); //var result = tests.Max(o=>o.Time);
var result = tests.OrderByDescending(o => o.Time).First(); Console.WriteLine(string.Format("the result is ID={0}, Name={1}, Score={2}, Time={3}", result.ID, result.Name, result.Score, result.Time));
select max(Time) as Time from tablename where ID in (1,3,4,5)