2017-05-18 17 views
0

항목은 모든 목록에있을 수 있습니다. 하지만 모든 아이템은 한 번만. 다른 목록 중 하나에 같은 (또는 유사한) 항목이 없어야합니다.목록의 7 개의 SortedLists 중 하나에서 익명 객체를 찾아 제거하는 방법은 무엇입니까?

여기 내 수업이 있습니다. 항목을 제거하려면

class Calendar 
{ 
    public List<SortedList<string, Item>> list; 

    public Item() 
    { 
     list = new List<SortedList<string, Item>>(); //List of Week 
     list.Add(new SortedList<string, Item>()); //Monday 
     list.Add(new SortedList<string, Item>()); //Tuesday 
     list.Add(new SortedList<string, Item>()); //Wednesday 
     list.Add(new SortedList<string, Item>()); //Thursday 
     list.Add(new SortedList<string, Item>()); //Friday 
     list.Add(new SortedList<string, Item>()); //Saturday 
     list.Add(new SortedList<string, Item>()); //Sunday 
    } 
} 

내가이 같은 시도 :

public void RemoveItem(string key) 
{ 
    foreach (SortedList<string, Item> item in list) 
    { 
     if (item.Keys.Contains(key)) 
     { 
      item.Remove(key); 
     } 
    } 
} 

을하지만 분명히 작동하지 않습니다.

다음은 항목을 추가하는 방법입니다. 그러나 그것은 명백해야한다 :

public void AddItem(string Day, Item i) 
    { 
     string s = i.Title 
     switch (Tag) 
     { 
      case "Monday": 
       list[0].Add(s, i); 
       break; 
      case "Tuesday": 
       list[1].Add(s, i); 
       break; 
      case "Wednesday": 
       list[2].Add(s, i); 
       break; 
      case "Thursday": 
       list[3].Add(s, i); 
       break; 
      case "Friday": 
       list[4].Add(s, i); 
       break; 
      case "Saturday": 
       list[5].Add(s, i); 
       break; 
      case "Sunday": 
       list[6].Add(s, i); 
       break; 
      default: 
       list[0].Add(s, i); 
       break; 
     } 
    } 

은 처음에는 List<List<Item>>을 사용하지만 난 SortedList 내 목적을 위해 쉽게 처리 할 수 ​​있음을 알아 냈다.

+0

for 루프를 대신 사용해 보셨습니까? –

+0

예. 어느 쪽도 일하지 않았다. –

+0

두 번째 목록 인'foreach (list in var) {list.RemoveWhere (x => x.key == key);}'를 예제로 열거하고있는 것을 보지 못했습니다 ... –

답변

0

문제는 목록이 아닌 SortedKey에서 제거하는 것입니다. 나는 당신의 의도는 그 의도 인 경우, 목록에서 SortedList를 제거하는 것입니다 해석 할 수있는 한 가지 방법은 다음과 같습니다

 private void GoHere() 
     { 
      var list = new List<SortedList<string, string>>(); //List of Week 
      list.Add(new SortedList<string, string>() {{"monday", "monday"}}); //Monday 
      list.Add(new SortedList<string, string>() {{"tues", "monday"}}); //Tuesday 
      list.Add(new SortedList<string, string>() {{"wed", "monday"}}); //Wednesday 
      list.Add(new SortedList<string, string>() {{"thurs", "monday"}}); //Thursday 
      list.Add(new SortedList<string, string>() {{"fri", "monday"}}); //Friday 
      list.Add(new SortedList<string, string>() {{"sun", "monday"}}); //Saturday 
      int? index = null; 

      for (int i = 0; i < list.Count; i++) 
      { 
       SortedList<string, string> sortedList = list[i]; 
       if (sortedList.ContainsKey("wed")) 
       { 
        index = i; 
        break; 
       } 
      } 
      if (index.HasValue) 
      { 
       list.RemoveAt(index.Value); 
      } 
     } 
+0

나의 의도는 SortedLists 중 하나 안에있는 하나의 Item을 제거하는 것이다. 하지만 먼저 (SortedList가 내부에 있는지 모르기 때문에) 항목을 찾아야합니다. –

0

좋아요. 나는 해결책을 찾아 냈다. 내 논리에는 문제가 없었습니다.

string[] seperators = { "|" }; 
string[] s = "Name | Title".Split(seperators, StringSplitOptions.RemoveEmptyEntries); 
Calendar.RemoveItem(s[1]); 

을하지만 apperently 공간은 문자열 전에 유지 :

내가 찾아 항목을 삭제하는 데 사용되는 문자열은에서왔다. 그래서이 코드를 추가하고 지금 작동합니다 :

string title = s[2].Remove(0, 1); 
Calendar.RemoveItem(title); 
+0

Sidenote :'myString.Trim()'은 문자열의 시작과 끝 모두에서 공백 문자와 개행 문자를 모두 자동으로 제거합니다 (가운데는 제외). 문자열의 한 면만 자르고 싶다면'TrimStart()'와'.TrimEnd()'도 있습니다. – Flater