获取entityframework中每个组的最后记录?

时间:2017-02-16 13:04:16

标签: c# asp.net-mvc entity-framework linq linq-to-entities

我想检索实体框架中每个 MobileNo 的最后插入记录。

这是我的表数据。

ID      RegNo       MobileNo    CreatedDate
26727   190077348   9696562673  13-02-2017 06:31
26729   123782783   9696562673  13-02-2017 06:35
45779   530087328   5878525875  07-02-2017 07:23
99902   120058572   7379130560  08-02-2017 12:39
64477   180073650   7417516480  10-02-2017 13:47
81839   240087264   7754990580  11-02-2017 10:47 

并希望输出

ID      RegNo       MobileNo    CreatedDate 
26729   123782783   9696562673  13-02-2017 06:35
45779   530087328   5878525875  07-02-2017 07:23 
99902   120058572   7379130560  08-02-2017 12:39
64477   180073650   7417516480  10-02-2017 13:47
81839   240087264   7754990580  11-02-2017 10:47 

2 个答案:

答案 0 :(得分:14)

假设您的表名为table = bmw.DataTable(source=source, columns=columns, height = 1000, width = 4000)

Items

正在运行示例:https://dotnetfiddle.net/3ud2pB

答案 1 :(得分:1)

试试这个。

;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY MobileNo ORDER BY CreatedDate DESC) AS rn
   FROM yourtablename
)
SELECT *
FROM cte
WHERE rn = 1