简体   繁体   English

如何最有效地从数据表中删除条目?

[英]How do I most efficiently remove entries from a DataTable?

I'm developing on a more or less legacy C# application and stumbled upon a code that I wrote some time ago. 我正在开发一个或多或少的旧式C#应用程序,偶然发现了我前一段时间编写的代码。 But somehow I get the feeling, that this is not really a good approach to this. 但是我不知何故感觉到这并不是一个好方法。

How would I make this code better? 我如何使这段代码更好?

        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();
    }

Thanks in advance, bb 预先感谢,bb

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM