此行已从表中删除,并且没有任何数据

时间:2015-04-01 09:48:53

标签: c# sql linq

此行已从表中删除,但没有任何数据。 BeginEdit()将允许在此行中创建新数据。

我有一种方法,我尝试删除所有重复的行(使用日期),并且我的代码获得了上述异常。我理解异常意味着什么,但我不明白我在代码中做错了什么导致它。我已经多次看过它而没有运气。

            using (OldStockRatingsTableAdapter ratingsAdapter = new OldStockRatingsTableAdapter())
            using (DataSet.OldStockRatingsDataTable ratingsTable = new DataSet.OldStockRatingsDataTable())
            {
                ratingsAdapter.ClearBeforeFill = true;
                ratingsAdapter.Adapter.UpdateBatchSize = 500;
                ratingsAdapter.FillByMarketSymbol(ratingsTable, market, symbol);

                var masterQuery = from c in ratingsTable
                            where c.Symbol == symbol && c.Market == market
                            select c;
                List<DataSet.OldStockRatingsRow> masterRows = masterQuery.ToList();
                List<DataSet.OldStockRatingsRow> masterDistinctRows = masterQuery.DistinctBy(i => i.Date).ToList();

                for (int i = 0; i < masterDistinctRows.Count; i++)
                {
                    var dateQuery = from c in masterRows
                                    where c.Date == masterDistinctRows.ElementAtOrDefault(i).Date
                                    select c;
                    List<DataSet.OldStockRatingsRow> dateRow = dateQuery.ToList(); // getting the exception here

                    if (dateRow.Count > 1)
                    {
                        for (int j = 1; j < dateRow.Count; j++)
                        {
                            ratingsTable.RemoveOldStockRatingsRow(dateRow.ElementAtOrDefault(j));
                            Console.WriteLine("Stock rating deleted for " + symbol + " in the " + market + " market!");
                        }
                    }
                }

                // update everything here
                DataSet.OldStockRatingsDataTable tempRatingsTable = new DataSet.OldStockRatingsDataTable();
                tempRatingsTable = (DataSet.OldStockRatingsDataTable)ratingsTable.GetChanges();

                if (tempRatingsTable != null)
                {
                    ratingsAdapter.Adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
                    ratingsAdapter.Adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
                    ratingsAdapter.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
                    ratingsAdapter.Update(tempRatingsTable);
                    tempRatingsTable.Dispose();
                    Console.WriteLine("Stock rating calculations finished for " + symbol + " in the " + market + " market!");
                }
 }

1 个答案:

答案 0 :(得分:1)

我不知道你的代码,但如果你要做的就是删除重复项,那么maby这段代码对你有用:

delete duplicate query
with x as (
select *, row_number() over(partition by <COLUMN>, checksum(<Columns to validate>) order by <COLUMN>) as rn
from <TABLE>
)
delete x where rn > 1
//it will validate multiple columns if more are given

注意:此代码不是由我制作的,我会链接原始帖子here。该代码由@dean发布。