How to Select Min and Max date values in Linq Query

后端 未结 3 1824
离开以前
离开以前 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: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.

提交回复
热议问题