2017-12-03 4 views
0

現在、私はOutlookで作業しており、項目のリストを繰り返しています。リスト/コレクションから特定のタイプの最後のアイテムを取得するには?

問題があります: 現在のアイテムが最後のアイテムである場合は、foreach-loopで質問しますが、この種の問題は解決できません。

これは私のコードです:

lastAppointmentItem = myAppointmentItems.GetLast(); 
foreach (Outlook.AppointmentItem myItem in myAppointmentItems) 
{ 
    // Types 
    // myAppointmentItems = Outlook.Items 
    // myItem = Outlook.AppointmentItem 
    if(myItem.Equals(lastAppointmentItem)) { do_something(); } 
} 

私は

System.Runtime.InteropServices.COMException: 
// The last element of the list cannot be called 

を取得するプログラムを実行すると、どのように私は私の問題を解決することができます - あなたたちは知っていますか?

ありがとうございます!

ご挨拶。

答えて

0

System.Linqクラスを使用して.Last()メソッドの使用を取得できました。私は下の例が働いています。

namespace StackOverflow 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     List<Test> tests = new List<Test>() 
     { 
      new StackOverflow.Test() 
      { 
       id = "test", 
       name = "name" 
      }, 
      new StackOverflow.Test() 
      { 
       id = "test1", 
       name = "name1" 
      }, 
      new StackOverflow.Test() 
      { 
       id = "test2", 
       name = "name2" 
      }, 
     }; 

     var lastItem = tests.Last(); 
     foreach(Test test in tests) 
     { 
      if (test.Equals(lastItem)) 
      { 
       Console.WriteLine("last item has been cycled to"); 
      } 
     } 
     Console.ReadLine();    
     } 
    } 

    public class Test 
    { 
    public string id { get; set; } 
    public string name { get; set; } 
    } 
} 
0

1からmyAppointmentItemsまでの "for"ループを使用して "foreach"ループを使用しないでください。意味のある順序を持つように並べ替えることを忘れないでください(myAppointmentItems.Sort(...))。

関連する問題