2017-12-03 13 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(...)).