日期:2014-05-20 浏览次数:20860 次
var query= db.TableA.GroupBy(a=>System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(a.Birthday,DateTime.Now))
.OrderBy(g=>System.Math.Abs(g.Key)).Select(g=>g.First()).Take(5);
void Main()
{
var list=new List<A>
{
new A{ID=1, Birthday=DateTime.Now.Date},
new A{ID=2, Birthday=new DateTime(2013,11,13)},
new A{ID=3, Birthday=new DateTime(2013,11,10)},
new A{ID=4, Birthday=new DateTime(2013,10,12)},
new A{ID=5, Birthday=new DateTime(2013,10,01)},
new A{ID=6, Birthday=new DateTime(2013,10,01)},
new A{ID=7, Birthday=new DateTime(2013,10,01)},
new A{ID=8, Birthday=new DateTime(2013,10,01)},
};
var query= list.GroupBy(a=>System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(a.Birthday,DateTime.Now))
.OrderBy(g=>System.Math.Abs(g.Key))
.Select(x=>new
{
ID=x.First().ID,
Birthday=x.First().Birthday.Date==DateTime.Now.Date?"今天":x.First().Birthday.Date==DateTime.Now.AddDays(1).Date?"明天":x.First().Birthday.ToString("MM-dd")
}).Take(5);
query.ToList().ForEach(q=>Console.WriteLine("{0}\t{1}",q.ID,q.Birthday));
/*
1 今天
2 明天
3 11-10
4 10-12
5 10-01
*/
}
class A
{
public int ID{get;set;}
public DateTime Birthday{get;set;}
}