i have a table
Id Number
1   9
2   10
3   12
4   19
5   20
select Id where Number is closest to 18 it should return row 4开发者_StackOverflow中文版 which is 19
how do i write this in linq and tsql? thanks
(from q in table
 orderby Math.Abs(18 - q.Number)
 select q).FirstOrDefault()
and
SELECT TOP 1
    *
FROM
    table
ORDER BY
    ABS(10 - Number)
and for a datetime
var nearTo = new DateTime(1999, 12, 31);
(from q in table
 orderby Math.Abs((nearTo - q.Date).TotalSeconds)
 select q).FirstOrDefault()
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论