C#LinQ查询从数据表中提取前3条记录 [英] C# LinQ query to pull top 3 records from data table
问题描述
我需要写一个LinQ查询。请耐心等待我,因为我是.Net C#的新手,并遇到了一个场景,我想在客户端ID上执行
分区 / em>然后需要通过 UpdateDateTime 降序
执行
订单(即最新)列以获取该分区中的前3个记录。
I need to write a LinQ query. please bear with me as I'm new in .Net C# and came across one scenario wherein I want to perform Partition By on Client ID then need to perform order by UpdateDateTime descending (i.e. Latest) column to fetch Top 3 records in that partition.
输入数据 -   ; 地址
ClientID | Addresses_id | AddressID    |国家  |城市| PostalCode | BlockName | FloorNumber | UpdateDateTime
ClientID| Addresses_id| AddressID | State |City|PostalCode|BlockName|FloorNumber|UpdateDateTime
50       | 501                | 101 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-17
50 |501 |101 | NULL | PK | NULL | NULL | 7 | 2015-12-17
60       | 501                | 102 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-18
60 |501 |102 | NULL | PK | NULL | NULL | 7 | 2015-12-18
60       | 401                | 103 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-13
60 |401 |103 | NULL | PK | NULL | NULL | 7 | 2015-12-13
60     | 203                | 104 NBSP;             | NULL  | PK  | NULL        | NULL        | 7     
        | 2015-12- 05
60 |203 |104 | NULL | PK | NULL | NULL | 7 | 2015-12- 05
60     | 203                | 104 NBSP;             | NULL  | PK  | NULL        | NULL        | 7     
        | 2015-12-08
60 |203 |104 | NULL | PK | NULL | NULL | 7 | 2015-12- 08
预期输出 - ResultSet
Expected output - ResultSet
ClientID | Addresses_id | AddressID    |国家  |城市| PostalCode | BlockName | FloorNumber | UpdateDateTime
ClientID| Addresses_id| AddressID | State |City|PostalCode|BlockName|FloorNumber|UpdateDateTime
50       | 501                | 101 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-17
50 |501 |101 | NULL | PK | NULL | NULL | 7 | 2015-12-17
60       | 501                | 102 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-18
60 |501 |102 | NULL | PK | NULL | NULL | 7 | 2015-12-18
60       | 401                | 103 NBSP;             | NULL  | PK  | NULL        | NULL        | 7   
          | 2015-12-13
60 |401 |103 | NULL | PK | NULL | NULL | 7 | 2015-12-13
60     | 203                | 104 NBSP;             | NULL  | PK  | NULL        | NULL        | 7     
        | 2015-12-08
60 |203 |104 | NULL | PK | NULL | NULL | 7 | 2015-12- 08
此处,cline_ID中最新的最新记录为3.
Here, maximum latest possible record in cline_ID are 3.
推荐答案
如果数据在数据库中,则进行分区,然后进行分区。没理由使用LINQ。
If the data is in a DB then do the partitioning and whatnot there. No reason to use LINQ.
我相信当你说分区时,你的意思是按客户ID分组。然后,对于要在更新日期之前订购的每个客户,然后为每个客户端获取前3个。您没有提供有关数据结构的任何信息,因此我假设您使用DataTable是
。除了访问字段之外,它并不重要。
I believe when you say partition you mean group by the client ID. Then for each client you want to order by the update date and then grab the first 3 for each. You didn't provide any information about the structure of your data so I'm going to assume you're using a DataTable. It doesn't really matter other than for accessing the fields.
//Breaking this into separate queries for easier reading
//data is the DataTable containing your data
//Group by clients
var clients = from r in data.Rows.OfType<DataRow>()
group r by r.Field<int>("ClientID") into g
select new { ClientId = g.Key, Items = g.ToArray() };
//For each client get the top 3
var itemsByClients = from client in clients
select client.Items.OrderByDescending(i => i.Field<DateTime>("UpdateDateTime")).Take(3);
//itemsByClients is a list of clients where each client has the top 3 rows, you can then combine them into a single list if you want
这篇关于C#LinQ查询从数据表中提取前3条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!