搜索

2014-11-05 64 views
1

的縮小結果發起了主題在這裏已經成功地篩選,但需要縮小搜索

Line Stream Reader

我日誌包含以下信息

2014/11/03 00:00:00 PID:11484 UUID:231d09e2-56d9-43c1-8170-e45d43b56e4a Start E:/I3/IC/Logs/2014-11-03/ip.ininlog 
2014/11/03 06:02:11 PID:11484 UUID:231d09e2-56d9-43c1-8170-e45d43b56e4a End E:/I3/IC/Logs/2014-11-03/ip.ininlog 
2014/11/03 06:02:11 PID:11484 UUID:fe49e587-6b40-446c-9b06-d16bae28683e Start E:/I3/IC/Logs/2014-11-03/ip_1.ininlog 
2014/11/03 06:20:13 PID:11484 UUID:fe49e587-6b40-446c-9b06-d16bae28683e End E:/I3/IC/Logs/2014-11-03/ip_1.ininlog 
2014/11/03 06:20:13 PID:11484 UUID:3ec28198-f79e-4d36-90f9-5004ebe2ab91 Start E:/I3/IC/Logs/2014-11-03/ip_2.ininlog 
2014/11/03 06:35:29 PID:11484 UUID:3ec28198-f79e-4d36-90f9-5004ebe2ab91 End E:/I3/IC/Logs/2014-11-03/ip_2.ininlog 
2014/11/03 06:35:29 PID:11484 UUID:ede8160a-d639-450c-8ccf-7f2c2a6c7823 Start E:/I3/IC/Logs/2014-11-03/ip_3.ininlog 
2014/11/03 06:49:41 PID:11484 UUID:ede8160a-d639-450c-8ccf-7f2c2a6c7823 End E:/I3/IC/Logs/2014-11-03/ip_3.ininlog 
2014/11/03 06:49:41 PID:11484 UUID:c53a53e1-c81c-4d5b-8256-7d6ba2d886ff Start E:/I3/IC/Logs/2014-11-03/ip_4.ininlog 
2014/11/03 07:03:20 PID:11484 UUID:c53a53e1-c81c-4d5b-8256-7d6ba2d886ff End E:/I3/IC/Logs/2014-11-03/ip_4.ininlog 
2014/11/03 07:03:20 PID:11484 UUID:be34b82a-a57f-4607-a638-b4f3dfca5617 Start E:/I3/IC/Logs/2014-11-03/ip_5.ininlog 
2014/11/03 07:16:15 PID:11484 UUID:be34b82a-a57f-4607-a638-b4f3dfca5617 End E:/I3/IC/Logs/2014-11-03/ip_5.ininlog 
2014/11/03 07:16:15 PID:11484 UUID:7895acc1-29dc-4412-b1b8-34642d4a8752 Start E:/I3/IC/Logs/2014-11-03/ip_6.ininlog 
2014/11/03 07:28:07 PID:11484 UUID:7895acc1-29dc-4412-b1b8-34642d4a8752 End E:/I3/IC/Logs/2014-11-03/ip_6.ininlog 
2014/11/03 07:28:07 PID:11484 UUID:72f09dc3-3e6e-44ce-a419-32679566bbb2 Start E:/I3/IC/Logs/2014-11-03/ip_7.ininlog 
2014/11/03 07:40:33 PID:11484 UUID:72f09dc3-3e6e-44ce-a419-32679566bbb2 End E:/I3/IC/Logs/2014-11-03/ip_7.ininlog 
2014/11/03 07:40:33 PID:11484 UUID:29c1ec2e-7ab1-4d48-baf4-7b8ff8e2f04a Start E:/I3/IC/Logs/2014-11-03/ip_8.ininlog 
2014/11/03 07:53:03 PID:11484 UUID:29c1ec2e-7ab1-4d48-baf4-7b8ff8e2f04a End E:/I3/IC/Logs/2014-11-03/ip_8.ininlog 
2014/11/03 07:53:03 PID:11484 UUID:aee440b6-05d5-426f-b7ba-cb17953d1331 Start E:/I3/IC/Logs/2014-11-03/ip_9.ininlog 
2014/11/03 08:04:20 PID:11484 UUID:aee440b6-05d5-426f-b7ba-cb17953d1331 End E:/I3/IC/Logs/2014-11-03/ip_9.ininlog 
2014/11/03 08:04:20 PID:11484 UUID:b8bbcd9e-9033-4271-9b2e-e43a3c528772 Start E:/I3/IC/Logs/2014-11-03/ip_10.ininlog 
2014/11/03 08:15:09 PID:11484 UUID:b8bbcd9e-9033-4271-9b2e-e43a3c528772 End E:/I3/IC/Logs/2014-11-03/ip_10.ininlog 
2014/11/03 08:15:09 PID:11484 UUID:fb8b70de-1725-47e0-9757-8a6ade8355a1 Start E:/I3/IC/Logs/2014-11-03/ip_11.ininlog 
2014/11/03 08:25:17 PID:11484 UUID:fb8b70de-1725-47e0-9757-8a6ade8355a1 End E:/I3/IC/Logs/2014-11-03/ip_11.ininlog 
2014/11/03 08:25:17 PID:11484 UUID:0c5fec25-6b71-4240-8e7c-9333ecce9a3e Start E:/I3/IC/Logs/2014-11-03/ip_12.ininlog 
2014/11/03 08:35:01 PID:11484 UUID:0c5fec25-6b71-4240-8e7c-9333ecce9a3e End E:/I3/IC/Logs/2014-11-03/ip_12.ininlog 
2014/11/03 08:35:01 PID:11484 UUID:68c1e9c9-1797-4b38-be41-d7675b847e0b Start E:/I3/IC/Logs/2014-11-03/ip_13.ininlog 
2014/11/03 08:45:37 PID:11484 UUID:68c1e9c9-1797-4b38-be41-d7675b847e0b End E:/I3/IC/Logs/2014-11-03/ip_13.ininlog 
2014/11/03 08:45:37 PID:11484 UUID:883bf2bd-9731-4709-9c39-5b40f518a1d5 Start E:/I3/IC/Logs/2014-11-03/ip_14.ininlog 
2014/11/03 08:54:09 PID:11484 UUID:883bf2bd-9731-4709-9c39-5b40f518a1d5 End E:/I3/IC/Logs/2014-11-03/ip_14.ininlog 
2014/11/03 08:54:09 PID:11484 UUID:9b925501-e0c0-469c-9336-59d742e80c6b Start E:/I3/IC/Logs/2014-11-03/ip_15.ininlog 
2014/11/03 09:04:32 PID:11484 UUID:9b925501-e0c0-469c-9336-59d742e80c6b End E:/I3/IC/Logs/2014-11-03/ip_15.ininlog 
2014/11/03 09:04:32 PID:11484 UUID:a5e850d0-5989-4ad4-bd97-c47cdca444b7 Start E:/I3/IC/Logs/2014-11-03/ip_16.ininlog 
2014/11/03 09:15:00 PID:11484 UUID:a5e850d0-5989-4ad4-bd97-c47cdca444b7 End E:/I3/IC/Logs/2014-11-03/ip_16.ininlog 
2014/11/03 09:15:00 PID:11484 UUID:fde960e3-1bda-469a-9c4d-fc1921d0e57e Start E:/I3/IC/Logs/2014-11-03/ip_17.ininlog 
2014/11/03 09:25:28 PID:11484 UUID:fde960e3-1bda-469a-9c4d-fc1921d0e57e End E:/I3/IC/Logs/2014-11-03/ip_17.ininlog 
2014/11/03 09:25:28 PID:11484 UUID:eba5a083-13fa-42e3-a16b-c01a6ca0287f Start E:/I3/IC/Logs/2014-11-03/ip_18.ininlog 
2014/11/03 09:35:57 PID:11484 UUID:eba5a083-13fa-42e3-a16b-c01a6ca0287f End E:/I3/IC/Logs/2014-11-03/ip_18.ininlog 
2014/11/03 09:35:57 PID:11484 UUID:51a5b17b-6b23-4087-a3e6-f1b001ae32b9 Start E:/I3/IC/Logs/2014-11-03/ip_19.ininlog 
2014/11/03 09:45:53 PID:11484 UUID:51a5b17b-6b23-4087-a3e6-f1b001ae32b9 End E:/I3/IC/Logs/2014-11-03/ip_19.ininlog 
2014/11/03 09:45:53 PID:11484 UUID:6f0f7bd3-7081-4036-8864-905a118db2b9 Start E:/I3/IC/Logs/2014-11-03/ip_20.ininlog 
2014/11/03 09:55:52 PID:11484 UUID:6f0f7bd3-7081-4036-8864-905a118db2b9 End E:/I3/IC/Logs/2014-11-03/ip_20.ininlog 
2014/11/03 09:55:52 PID:11484 UUID:83c5ad5f-b3b5-4167-b8e2-3a75b6162baf Start E:/I3/IC/Logs/2014-11-03/ip_21.ininlog 
2014/11/03 10:04:50 PID:11484 UUID:83c5ad5f-b3b5-4167-b8e2-3a75b6162baf End E:/I3/IC/Logs/2014-11-03/ip_21.ininlog 
2014/11/03 10:04:50 PID:11484 UUID:58abf277-edb1-48ef-a0d1-ce9a2c53daca Start E:/I3/IC/Logs/2014-11-03/ip_22.ininlog 
2014/11/03 10:13:46 PID:11484 UUID:58abf277-edb1-48ef-a0d1-ce9a2c53daca End E:/I3/IC/Logs/2014-11-03/ip_22.ininlog 
2014/11/03 10:13:46 PID:11484 UUID:6e9c61db-dd62-453b-bfa8-6d0e23fb428a Start E:/I3/IC/Logs/2014-11-03/ip_23.ininlog 
2014/11/03 10:23:30 PID:11484 UUID:6e9c61db-dd62-453b-bfa8-6d0e23fb428a End E:/I3/IC/Logs/2014-11-03/ip_23.ininlog 
2014/11/03 10:23:30 PID:11484 UUID:16ea52aa-9f78-46b6-913b-308dbfe1ad4f Start E:/I3/IC/Logs/2014-11-03/ip_24.ininlog 
2014/11/03 10:33:50 PID:11484 UUID:16ea52aa-9f78-46b6-913b-308dbfe1ad4f End E:/I3/IC/Logs/2014-11-03/ip_24.ininlog 
2014/11/03 10:33:50 PID:11484 UUID:8b7b310c-657a-4665-842e-7fcb40dc8df1 Start E:/I3/IC/Logs/2014-11-03/ip_25.ininlog 
2014/11/03 10:43:55 PID:11484 UUID:8b7b310c-657a-4665-842e-7fcb40dc8df1 End E:/I3/IC/Logs/2014-11-03/ip_25.ininlog 
2014/11/03 10:43:55 PID:11484 UUID:4e707790-18ff-4ba8-93d5-d198e978f63f Start E:/I3/IC/Logs/2014-11-03/ip_26.ininlog 
2014/11/03 10:54:10 PID:11484 UUID:4e707790-18ff-4ba8-93d5-d198e978f63f End E:/I3/IC/Logs/2014-11-03/ip_26.ininlog 
2014/11/03 10:54:10 PID:11484 UUID:176c4bca-84fb-4361-9898-f500edc348bb Start E:/I3/IC/Logs/2014-11-03/ip_27.ininlog 
2014/11/03 11:05:09 PID:11484 UUID:176c4bca-84fb-4361-9898-f500edc348bb End E:/I3/IC/Logs/2014-11-03/ip_27.ininlog 
2014/11/03 11:05:09 PID:11484 UUID:70afef6a-fcd3-4fac-aaf6-6c419b2bfbba Start E:/I3/IC/Logs/2014-11-03/ip_28.ininlog 
2014/11/03 11:14:38 PID:11484 UUID:70afef6a-fcd3-4fac-aaf6-6c419b2bfbba End E:/I3/IC/Logs/2014-11-03/ip_28.ininlog 
2014/11/03 11:14:38 PID:11484 UUID:09c4b5ec-c88a-4687-a0c5-84b6bbd402cd Start E:/I3/IC/Logs/2014-11-03/ip_29.ininlog 
2014/11/03 11:24:06 PID:11484 UUID:09c4b5ec-c88a-4687-a0c5-84b6bbd402cd End E:/I3/IC/Logs/2014-11-03/ip_29.ininlog 
2014/11/03 11:24:06 PID:11484 UUID:bca881be-4310-447e-89d1-21af0d1c528a Start E:/I3/IC/Logs/2014-11-03/ip_30.ininlog 
2014/11/03 11:34:22 PID:11484 UUID:bca881be-4310-447e-89d1-21af0d1c528a End E:/I3/IC/Logs/2014-11-03/ip_30.ininlog 
2014/11/03 11:34:22 PID:11484 UUID:424c6ca2-6ede-468a-868a-4372c4b0556c Start E:/I3/IC/Logs/2014-11-03/ip_31.ininlog 
2014/11/03 11:45:01 PID:11484 UUID:424c6ca2-6ede-468a-868a-4372c4b0556c End E:/I3/IC/Logs/2014-11-03/ip_31.ininlog 
2014/11/03 11:45:01 PID:11484 UUID:b5458703-74e4-41d9-ab21-2cbc8576389f Start E:/I3/IC/Logs/2014-11-03/ip_32.ininlog 
2014/11/03 11:56:15 PID:11484 UUID:b5458703-74e4-41d9-ab21-2cbc8576389f End E:/I3/IC/Logs/2014-11-03/ip_32.ininlog 
2014/11/03 11:56:15 PID:11484 UUID:337458a2-85a2-47c7-a101-73ac489ed3bf Start E:/I3/IC/Logs/2014-11-03/ip_33.ininlog 
2014/11/03 12:06:44 PID:11484 UUID:337458a2-85a2-47c7-a101-73ac489ed3bf End E:/I3/IC/Logs/2014-11-03/ip_33.ininlog 
2014/11/03 12:06:44 PID:11484 UUID:8a1aef00-f1e3-4060-b24c-35ef77e905bf Start E:/I3/IC/Logs/2014-11-03/ip_34.ininlog 
2014/11/03 12:17:26 PID:11484 UUID:8a1aef00-f1e3-4060-b24c-35ef77e905bf End E:/I3/IC/Logs/2014-11-03/ip_34.ininlog 
2014/11/03 12:17:26 PID:11484 UUID:dde16219-46bb-4d6a-8387-ef05886cb828 Start E:/I3/IC/Logs/2014-11-03/ip_35.ininlog 
2014/11/03 12:28:30 PID:11484 UUID:dde16219-46bb-4d6a-8387-ef05886cb828 End E:/I3/IC/Logs/2014-11-03/ip_35.ininlog 
2014/11/03 12:28:30 PID:11484 UUID:82c1e793-a963-4e56-ab00-ccb75b90e68c Start E:/I3/IC/Logs/2014-11-03/ip_36.ininlog 
2014/11/03 12:39:35 PID:11484 UUID:82c1e793-a963-4e56-ab00-ccb75b90e68c End E:/I3/IC/Logs/2014-11-03/ip_36.ininlog 
2014/11/03 12:39:35 PID:11484 UUID:2adce285-9689-43b1-835a-a7402a4e8f89 Start E:/I3/IC/Logs/2014-11-03/ip_37.ininlog 
2014/11/03 12:51:09 PID:11484 UUID:2adce285-9689-43b1-835a-a7402a4e8f89 End E:/I3/IC/Logs/2014-11-03/ip_37.ininlog 
2014/11/03 12:51:09 PID:11484 UUID:7ea39118-a311-44b2-a895-7388dfaeb1fd Start E:/I3/IC/Logs/2014-11-03/ip_38.ininlog 
2014/11/03 13:03:19 PID:11484 UUID:7ea39118-a311-44b2-a895-7388dfaeb1fd End E:/I3/IC/Logs/2014-11-03/ip_38.ininlog 
2014/11/03 13:03:19 PID:11484 UUID:7be575bb-4322-4416-b1ae-f464938cf6ce Start E:/I3/IC/Logs/2014-11-03/ip_39.ininlog 
2014/11/03 13:15:30 PID:11484 UUID:7be575bb-4322-4416-b1ae-f464938cf6ce End E:/I3/IC/Logs/2014-11-03/ip_39.ininlog 
2014/11/03 13:15:30 PID:11484 UUID:6799a8a8-de76-4832-8fad-a829354c8a4a Start E:/I3/IC/Logs/2014-11-03/ip_40.ininlog 
2014/11/03 13:27:41 PID:11484 UUID:6799a8a8-de76-4832-8fad-a829354c8a4a End E:/I3/IC/Logs/2014-11-03/ip_40.ininlog 
2014/11/03 13:27:41 PID:11484 UUID:f0945238-5ed6-4e1f-9990-8f61ad81bc15 Start E:/I3/IC/Logs/2014-11-03/ip_41.ininlog 
2014/11/03 13:39:56 PID:11484 UUID:f0945238-5ed6-4e1f-9990-8f61ad81bc15 End E:/I3/IC/Logs/2014-11-03/ip_41.ininlog 
2014/11/03 13:39:56 PID:11484 UUID:02eef369-6d38-4d28-b2e1-666adebea9a3 Start E:/I3/IC/Logs/2014-11-03/ip_42.ininlog 
2014/11/03 13:53:05 PID:11484 UUID:02eef369-6d38-4d28-b2e1-666adebea9a3 End E:/I3/IC/Logs/2014-11-03/ip_42.ininlog 
2014/11/03 13:53:05 PID:11484 UUID:3a1df262-d892-4ede-8152-07403c5b1ae1 Start E:/I3/IC/Logs/2014-11-03/ip_43.ininlog 
2014/11/03 14:06:32 PID:11484 UUID:3a1df262-d892-4ede-8152-07403c5b1ae1 End E:/I3/IC/Logs/2014-11-03/ip_43.ininlog 
2014/11/03 14:06:32 PID:11484 UUID:59aa8395-d6fd-4210-8b3f-8008a057d2d5 Start E:/I3/IC/Logs/2014-11-03/ip_44.ininlog 
2014/11/03 14:21:19 PID:11484 UUID:59aa8395-d6fd-4210-8b3f-8008a057d2d5 End E:/I3/IC/Logs/2014-11-03/ip_44.ininlog 
2014/11/03 14:21:19 PID:11484 UUID:ee0b747a-dfe7-4c6f-825a-93c446da8aab Start E:/I3/IC/Logs/2014-11-03/ip_45.ininlog 
2014/11/03 14:37:13 PID:11484 UUID:ee0b747a-dfe7-4c6f-825a-93c446da8aab End E:/I3/IC/Logs/2014-11-03/ip_45.ininlog 
2014/11/03 14:37:13 PID:11484 UUID:f834b1c0-a875-49e7-9bef-cae5be12f424 Start E:/I3/IC/Logs/2014-11-03/ip_46.ininlog 
2014/11/03 14:53:46 PID:11484 UUID:f834b1c0-a875-49e7-9bef-cae5be12f424 End E:/I3/IC/Logs/2014-11-03/ip_46.ininlog 
2014/11/03 14:53:46 PID:11484 UUID:21ac9763-7d47-48d5-8d1e-4568d2105bc5 Start E:/I3/IC/Logs/2014-11-03/ip_47.ininlog 
2014/11/03 15:10:30 PID:11484 UUID:21ac9763-7d47-48d5-8d1e-4568d2105bc5 End E:/I3/IC/Logs/2014-11-03/ip_47.ininlog 
2014/11/03 15:10:30 PID:11484 UUID:77f146b0-fc5a-4b41-9795-4cf733b8f254 Start E:/I3/IC/Logs/2014-11-03/ip_48.ininlog 
2014/11/03 15:30:23 PID:11484 UUID:77f146b0-fc5a-4b41-9795-4cf733b8f254 End E:/I3/IC/Logs/2014-11-03/ip_48.ininlog 
2014/11/03 15:30:23 PID:11484 UUID:bf1d62ab-4a66-4b44-bc91-2b27d26cde81 Start E:/I3/IC/Logs/2014-11-03/ip_49.ininlog 
2014/11/03 15:54:12 PID:11484 UUID:bf1d62ab-4a66-4b44-bc91-2b27d26cde81 End E:/I3/IC/Logs/2014-11-03/ip_49.ininlog 
2014/11/03 15:54:12 PID:11484 UUID:b0bbf12a-0d5f-43e0-ba3c-f18bcd04c7d7 Start E:/I3/IC/Logs/2014-11-03/ip_50.ininlog 
2014/11/03 16:19:26 PID:11484 UUID:b0bbf12a-0d5f-43e0-ba3c-f18bcd04c7d7 End E:/I3/IC/Logs/2014-11-03/ip_50.ininlog 
2014/11/03 16:19:26 PID:11484 UUID:8547bbbc-2c92-4878-a408-f62161e7a273 Start E:/I3/IC/Logs/2014-11-03/ip_51.ininlog 
2014/11/03 16:50:35 PID:11484 UUID:8547bbbc-2c92-4878-a408-f62161e7a273 End E:/I3/IC/Logs/2014-11-03/ip_51.ininlog 
2014/11/03 16:50:35 PID:11484 UUID:bba969c0-a2c9-423d-8ec6-a999cb5ca3b2 Start E:/I3/IC/Logs/2014-11-03/ip_52.ininlog 
2014/11/03 17:52:50 PID:11484 UUID:bba969c0-a2c9-423d-8ec6-a999cb5ca3b2 End E:/I3/IC/Logs/2014-11-03/ip_52.ininlog 
2014/11/03 17:52:50 PID:11484 UUID:aa5a2037-b42e-42af-aa9e-61e040d408c5 Start E:/I3/IC/Logs/2014-11-03/ip_53.ininlog 
2014/11/04 00:00:00 PID:11484 UUID:aa5a2037-b42e-42af-aa9e-61e040d408c5 End E:/I3/IC/Logs/2014-11-03/ip_53.ininlog 

如果文件找項目有一個23:00:00的時間它返回所有日誌到那個點,這不是我所需要的。我只需要在該時間範圍內的最後一個日誌,所以如果我使用23:00:00作爲點擊ip日誌文件的時間,它應該只告訴我,我需要ip_53日誌而不是全部53日誌。

我使用下面的代碼來完成我的搜索,到目前爲止

static TimeSpan? ExtractTime(string logLine) 
{ 
    var tokens = logLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); 
    if (tokens.Length < 2) 
    return null; 
    TimeSpan time; 
    if (!TimeSpan.TryParse(tokens[1], out time)) 
    return null; 
    return time; 
} 

static DateTime? ExtractDate(string logLine) 
{ 
    var tokens = logLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); 
    if (tokens.Length < 1) 
    return null; 
    DateTime date; 
    if (!DateTime.TryParse(tokens[0], out date)) 
    return null; 
    return date; 
} 

static void OutputLogLinesBeforeTime(string LogDir, string LogDate, string LogTime) 
{ 
    try 
    { 
    var time = TimeSpan.Parse(LogTime); 
    var date = DateTime.Parse(LogDate).Date; 
    DirectoryInfo d = new DirectoryInfo(LogDir + "\\" + LogDate + "\\"); 
    foreach (var file in d.GetFiles("*.ininlog_journal")) 
    { 
     try 
     { 
     using (Stream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
     using (StreamReader sReader = new StreamReader(stream)) 
     { 
      foreach (var line in sReader.EnumerateLines().Where(l => ExtractTime(l) <= time && ExtractDate(l) == date)) 
      callLogs.Add(line);   
     } 
     } 
     catch (UnauthorizedAccessException ae) 
     { 
     Console.WriteLine(ae.Message); 
     } 
     catch (SystemException se) 
     { 
     Console.WriteLine(se.Message); 
     } 
     catch (ApplicationException ape) 
     { 
     Console.WriteLine(ape.Message); 
     } 
     catch (Exception e) 
     { 
     Console.WriteLine(e.Message); 
     } 
    } 
    foreach (var e in callLogs) 
     Console.WriteLine(e); 
    } 
    catch (UnauthorizedAccessException ae) 
    { 
    Console.WriteLine(ae.Message); 
    } 
    catch (SystemException se) 
    { 
    Console.WriteLine(se.Message); 
    } 
    catch (ApplicationException ape) 
    { 
    Console.WriteLine(ape.Message); 
    } 
    catch (Exception e) 
    { 
    Console.WriteLine(e.Message); 
    } 
} 

回答

3

這條線:

foreach (var line in sReader.EnumerateLines().Where(l => ExtractTime(l) <= time && ExtractDate(l) == date)) 
     callLogs.Add(line); 

是迭代每一行的文件和存儲該行匹配任何行你在之後。如果你僅僅是最後一行符合您的標準,有興趣的你既可以用LINQ LastOrDefault方法訪問callLogs最後一個項目:

​​

或者你也可以直接搶最後一個項目不存儲任何插圖中:

string justOneRow = sReader.EnumerateLines().LastOrDefault(l => ExtractTime(l) <= time && ExtractDate(l) == date); 
Console.WriteLine(justOneRow); 

注意,Where你使用先前已更改爲LastOrDefault

這假定日誌文件是按日期順序的。如果他們不是,你將不得不在最後一個項目之前下單。

string justOneRow = sReader.EnumerateLines().Select(l => new 
{ 
    Date = ExtractDate(l), 
    Time = ExtractTime(l), 
    RowText = l 
}).Where(l => l.Time <= time && l.Date == date) 
.OrderBy(l => l.Date).ThenBy(l => l.Time).LastOrDefault().RowText; 
+0

非常感謝偉大的工作:在這種情況下,這樣你就不會調用ExtractDateExtractTime比你更需要我會創建一個匿名對象 – ondrovic 2014-11-05 18:19:07