简体   繁体   English

如何使用Linq到实体从列中检索最大的日期和时间?

[英]How can I retrieve the biggest date and time from the column using Linq to entity?

I have datetime column named DateMeasure in sql table. 我在SQL表中有一个名为DateMeasure datetime列。

I use entity framework 6 in my .net project. 我在.net项目中使用实体框架6。

Here is example of my table: 这是我的桌子的例子:

 Id           | NumberP  | TrackNumber | DateMeasure
28092|15240000|1.0.7.1782|2009040004731|2008-01-20 13:10:22.000
28094|61615000|1.0.7.1782|2009040007696|2010-05-20 13:11:38.000
28095|95317000|1.0.7.1782|2009040007695|2011-08-20 13:10:18.000
28101|15240000|1.0.7.1782|2009040004740|2015-11-20 14:10:22.000
28103|61615000|1.0.7.1782|2009040007690|2015-11-20 14:11:38.000
28104|95317000|1.0.7.1782|2009040007710|2009-02-20 14:10:18.000

The row that I want to get from table with help of Linq to entity: 我想借助Linq从表中获取到实体的行:

28103|61615000|1.0.7.1782|2009040007690|2015-11-20 14:11:38.000

How can I retrieve the biggest(latest) date and time from the column using Linq to entity? 如何使用Linq到实体从列中检索最大(最新) 日期和时间

You can get the max date using Max extension: 您可以使用Max扩展名获取最长日期:

var maxDate = context.Table.Max(m => m.DateMeasure);

Then, you can use FirstOrDefault to get that entity: 然后,您可以使用FirstOrDefault来获取该实体:

var model = context.Table.FirstOrDefault(m => m.DateMeasure == maxDate);

Actually, you can do it in just one query: 实际上,您只需要执行一个查询即可:

var model = context.Table.FirstOrDefault(m => m.DateMeasure == context.Table.Max(x => x.DateMeasure));

You can use the OrderBy methods of a IQuery object in Linq to sort by the DateMeasure column and fetch the first entry. 您可以在Linq中使用IQuery对象的OrderBy方法对DateMeasure列进行排序并获取第一个条目。

(from s in context.Table
       orderby s.DateMeasure descending).First()

Or with: 或搭配:

context.Table
       .OrderByDescending(s => s.DateMeasure)
       .First();

The OrderByDescending method will sort the query in descending order, you can also use OrderBy to sort by ascending order. OrderByDescending方法将按降序对查询进行排序,您也可以使用OrderBy按升序进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 linq 和实体框架访问表中特定列的所有值? - How can I access all values from a specific column in tables using linq and Entity Framework? 如何在 c# 中创建扩展以返回可以在 linq 中转换为实体的新日期时间条目? - How to I create an extension in c# to return a new date time entry that can be converted in linq to entity? 使用实体框架如何从表中检索集合? - Using Entity Framework how can I retrieve a collection from a table? 在Entity Framework或LINQ中进行选择时,如何将日期列分组为较不精确的格式? - How can I group a date column to a less precise format while selecting in Entity Framework or LINQ? 如何使用实体框架检索插入实体的 ID? - How can I retrieve Id of inserted entity using Entity framework? 如何在没有时间的情况下从表中检索日期列? - How to retrieve Date column from the table without Time? 如何使用linq(Entity Framework 5)更快地检索(获取)大量数据 - How to retrieve(get) huge amount of data using linq(Entity Framework 5) with faster time 如何使用LINQ从数组列表中检索一组唯一数组? - How can I retrieve a set of unique arrays from a list of arrays using LINQ? 如何在 linq 中比较没有时间部分的日期? - How can i compare date without time part in linq? 如何使用Entity Framework将当前日期/时间插入datetime数据类型字段? - How can I insert the current date/time into a datetime data type field using Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM