开发者

Datatable Recors

开发者 https://www.devze.com 2023-01-24 00:56 出处:网络
I have a datatable and it has 4 开发者_运维问答columns. My problem is some columns has same data, same date. I have to delete same data, same date. How can I delete dublicate data?

I have a datatable and it has 4 开发者_运维问答columns. My problem is some columns has same data, same date. I have to delete same data, same date. How can I delete dublicate data?

My Datatable: In this table I have to delete 1 or 3 (Id) In code side with for or foreach loop. Because in the same date there is a same Isban.

Id Name Isban Date

1 A 123 09.09.2010

2 B 123 10.09.2010

3 C 123 09.09.2010

4 A 234 11.09.2010

5 B 342 12.09.2010

Thanks You john


A standard way to do this is to run a select distinct query to insert the distinct records into a new table, delete the existing table, and then rename the new table to the previous table.

Edit: You can that you have to do this client-side.

One way is addressed here: Distinct in DataTable

Alternatively, loop through the table and store in a hash table each record; use the pair Isban/Date as the key and the record as the value. When you encounter a duplicate record it will already be in the hash table so you pass over it. Then, you can create a new data table from the records in the hash table.


If you DO have to do it in a loop, I would do it something like the following... Pre-query based on the minimum ID based on the given duplicate entitie elements, then delete for NOT being the minimum key

Select
      FldDup1,
      FldDup2,
      min( IDKey ) as KeepThisID,
      count(*) as TotalPerDupFields
   from
      YourTable
   group by
      FldDup1,
      FldDup2
   having
      TotalPerDupFields > 1

In this case, you'll end up with a sample result of...

FldDup1   FldDup2      KeepThisID    TotalPerDupFields
123       09.09.2010   1             2

as I was ignoring the 2nd column of "A", "B" and "C" as it didn't appear to be the indicator in your explanation of duplicates.

Then, I would isse a delete... via parameterized SQL-Delete query

Delete from YourTable
   Where FldDup1 = ResultQuery.FldDup1
     and FldDup2 = ResultQuery.FldDup2
     and NOT IDKey = ResultQuery.KeepThisID
0

精彩评论

暂无评论...
验证码 换一张
取 消