User Tools

Site Tools


blog:2025-04-07-001

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

blog:2025-04-07-001 [2025/04/07 17:34] (current)
jethro created
Line 1: Line 1:
 +~~NOTOC~~
 +====== 2025-04-07 排序好的時間List中找尋比輸入大的時間並回傳 ======
 +  * Require: C# List<​DateTime>​ gScheduleTimeList 中有一排序好的時間List,格式為 HH:​mm:​ss,現有一DateTime para_ScheduleTime 格式為HH:​mm:​ss要查詢於ListBox中比此輸入大的時間並回傳,若沒有比這個輸入的para_ScheduleTime於ListBox中,則取出於ListBox最小的時間
 +===== AI Answer =====
 +  * 你可以使用 LINQ 來查詢 gScheduleTimeList 中比 para_ScheduleTime 大的時間,並且如果沒有找到,則返回最小的時間。以下是一個示範的 C# 程式碼,展示了如何實現這一功能:
 +  * <sxh c#>using System;
 +using System.Collections.Generic;​
 +using System.Linq;​
 +
 +class Program
 +{
 +    static void Main()
 +    {
 +        // 假設 gScheduleTimeList 是一個已排序的 DateTime List
 +        List<​DateTime>​ gScheduleTimeList = new List<​DateTime>​
 +        {
 +            DateTime.Parse("​08:​00:​00"​),​
 +            DateTime.Parse("​09:​30:​00"​),​
 +            DateTime.Parse("​10:​15:​00"​),​
 +            DateTime.Parse("​12:​00:​00"​),​
 +            DateTime.Parse("​14:​30:​00"​)
 +        };
 +
 +        // 假設 para_ScheduleTime 是用戶輸入的時間
 +        DateTime para_ScheduleTime = DateTime.Parse("​10:​00:​00"​);​
 +
 +        // 查詢比 para_ScheduleTime 大的時間
 +        var largerTimes = gScheduleTimeList.Where(t => t.TimeOfDay > para_ScheduleTime.TimeOfDay).ToList();​
 +
 +        DateTime result;
 +        if (largerTimes.Any())
 +        {
 +            // 如果找到比 para_ScheduleTime 大的時間,取出第一個
 +            result = largerTimes.First();​
 +        }
 +        else
 +        {
 +            // 否則取出最小的時間
 +            result = gScheduleTimeList.Min();​
 +        }
 +
 +        // 輸出結果
 +        Console.WriteLine($"​結果時間:​ {result.ToString("​HH:​mm:​ss"​)}"​);​
 +    }
 +}</​sxh>​
 +===== 程式碼解釋 =====
 +  * 建立時間列表:gScheduleTimeList 是一個包含多個 DateTime 的已排序列表。
 +  * 用戶輸入的時間:para_ScheduleTime 是用戶輸入的時間。
 +  * 查詢比輸入時間大的時間:
 +  * 使用 LINQ 的 Where 方法過濾出比 para_ScheduleTime 大的時間。
 +===== 判斷結果: =====
 +  * 如果找到比 para_ScheduleTime 大的時間,取出第一個(最小的)。
 +  * 如果沒有找到,則取出 gScheduleTimeList 中的最小時間。
 +  * 顯示結果:最後將結果以 HH:mm:ss 格式輸出。
 +===== TAGS =====
 +  * 
 +====== ======
 +  * {{counter}} person(s) visited this page until now.
 +  * [[:​memo:​index|Back]]
 +====== ======
 +<​html><​!-- ​
 +PDF for A4-Portrait:​ {{pdfjs 50%,450px > xxx.pdf?​page-fit}}
 +PDF for A4-Landscape:​ {{pdfjs 500px,700px > xxx.pdf?​page-fit}}
 +PDF for iPad Note: {{pdfjs 700px,500px > xxx.pdf?​page-fit}}
 +
 +Youtube: {{youtube>​large:​XXXXX}}
 +Code Highlight: <sxh php; first-line: 70; highlight: [89,92]; title: New title attribute in action>
 +
 +--></​html>​
  
blog/2025-04-07-001.txt · Last modified: 2025/04/07 17:34 by jethro