2013-05-23 2 views
0

나는 다소 오래전의 C# 응용 프로그램을 개발 중이며 얼마 전에 작성한 코드를 발견했습니다. 그러나 어쨌든 나는 이것이 실제로 이것에 대한 좋은 접근법이 아니라는 느낌을 갖습니다.DataTable에서 항목을 가장 효율적으로 제거하려면 어떻게합니까?

이 코드를 어떻게 개선합니까? 사전에

 public static void RemoveEntries(DataTable source, ref DataTable destination, int indexSource, int indexDestination) 
    { 
     var arVals = new int[source.Rows.Count]; 
     var i = 0; 
     foreach (DataRow sourceRow in source.Rows) 
     { 
      if (sourceRow.RowState != DataRowState.Deleted) 
       arVals.SetValue(sourceRow[indexSource], i); 
      i += 1; 
     } 

     foreach (
      var destinationRow in 
      from DataRow row3 
       in destination.Rows 
      where arVals.Contains((int) row3[indexDestination]) 
      where row3.RowState != DataRowState.Deleted 
      select row3 
     ) 
      destinationRow.Delete(); 
    } 

감사합니다, BB

+0

대신 arVals를 사용하는, 당신은 단지 첫 번째 루프의 행을 삭제할 수 없습니다 ? 두 테이블에 존재하는 행을 고유하게 식별하는 일종의 기본 키 필드가 있습니까? – wgraham

+1

이와 같은 질문은 [Code Review] (http://codereview.stackexchange.com) 사이트에 더 적합 할 수 있습니다. –

+0

"코드가 더 좋음"은 코드 또는 성능이 낮다는 것을 의미합니까? – alexb

답변

1
public static void RemoveEntries(
    DataTable source, int sourceIndex, 
    DataTable destination, int destinationIndex) { 
    var query= 
     from DataRow rowDestination in destination.Rows 
     where rowDestination.RowState!=DataRowState.Deleted 
     from DataRow rowSource in source.Rows 
     where rowSource.RowState!=DataRowState.Deleted 
     where rowSource[sourceIndex]==rowDestination[destinationIndex] 
     select rowDestination; 

    foreach(var row in query.ToArray()) 
     row.Delete(); 
} 
관련 문제