2013-05-19 3 views
0

사전에 이미 좌표 세트가 있는지 확인하려면 어떻게합니까? SelectManager와 Where를 사용하고 있지만 카운트가 여전히 틀립니다. 나는 그것이 일반 목록의 값을 체크한다고 생각하지 않는다.Linq를 사용하여 Dictionary의 일반 목록에 값이 있는지 확인하십시오.

matchTile을 반복하고 일반 목록의 다른 모든 요소에서 조건 (질문의 일부가 아님)을 확인하고 싶습니다.

조건을 충족하면 동일한 그룹에 추가됩니다. 다음 반복이 발생하면 해당 요소가 이미 그룹에 추가되었는지 확인해야합니다. 그렇다면 건너 뜁니다. 그렇지 않은 경우 새 그룹 (List)을 만듭니다.

아무도 도와 줄 수 있습니까?

private void groupMatches(List<int[]> matchTile) 
{  
    Dictionary<int, List<int[]>> groups 
     = matchTile.GroupBy(line => new{x = line[0], y = line[1]}) 
        .Select((grp, index) => new{index, grp}) 
        .ToDictionary(info => info.index, info => info.grp.ToList()); 

    Debug.Log("groups: " + groups.Count); 
} 
:
private void groupMatches(List<int[]> matchTile){ 
    Dictionary<int, List<int[]>> groups = new Dictionary<int, List<int[]>>(); 
    int i = 0; 

    foreach(int[] coord in matchTile){ 
     var alreadyCounted = groups.SelectMany(x => x.Value).Where(x => x[0] == coord[0] && x[1] == coord[1]); 

     Debug.Log ("counted: " + alreadyCounted.Count()); 

     if(alreadyCounted.Count() > 0) continue; 

     // Create new group 
     groups.Add(i, new List<int[]>()); 
     groups[i].Add(coord); 

     foreach(int[] nextCoord in matchTile){ 
      if (...) 
      { 
       groups[i].Add(nextCoord); 
      } 
     } 

     i++; 
    } 

    Debug.Log ("groups: " + groups.Count); 
} 

답변

1

다음 코드를 사용해보십시오