0

我正在开发一个或多或少的遗留 C# 应用程序,并偶然发现了我前段时间编写的代码。但不知何故,我觉得这不是一个很好的方法。

我怎样才能使这段代码更好?

        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

4

1 回答 1

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();
}
于 2013-05-23T13:35:27.060 回答