How to Select Min and Max date values in Linq Query

后端 未结 3 1811
离开以前
离开以前 2020-12-16 09:11

I am moving from SQL to Linq and I need some help. I am testing both Linq-to-SQL and Linq-to-Entities. I want to try both to decide which one suits me best. Your help is app

相关标签:
3条回答
  • 2020-12-16 09:49
    dim mydate = from cv in mydata.t1s
      select cv.date1 asc
    
    datetime mindata = mydate[0];
    
    0 讨论(0)
  • 2020-12-16 09:55

    This should work for you

    //Retrieve Minimum Date
    var MinDate = (from d in dataRows select d.Date).Min();
    
    //Retrieve Maximum Date
    var MaxDate = (from d in dataRows select d.Date).Max(); 
    

    (From here)

    0 讨论(0)
  • 2020-12-16 09:58

    If you are looking for the oldest date (minimum value), you'd sort and then take the first item returned. Sorry for the C#:

    var min = myData.OrderBy( cv => cv.Date1 ).First();
    

    The above will return the entire object. If you just want the date returned:

    var min = myData.Min( cv => cv.Date1 );
    

    Regarding which direction to go, re: Linq to Sql vs Linq to Entities, there really isn't much choice these days. Linq to Sql is no longer being developed; Linq to Entities (Entity Framework) is the recommended path by Microsoft these days.

    From Microsoft Entity Framework 4 in Action (MEAP release) by Manning Press:

    What about the future of LINQ to SQL?

    It's not a secret that LINQ to SQL is included in the Framework 4.0 for compatibility reasons. Microsoft has clearly stated that Entity Framework is the recommended technology for data access. In the future it will be strongly improved and tightly integrated with other technologies while LINQ to SQL will only be maintained and little evolved.

    0 讨论(0)
提交回复
热议问题