c# - DataTable, How to conditionally delete rows -


I am busy with learning the C # and it is still going well. I still say my first hit "What?" moment.

The DataTable not only gives the random line access in its line archive, through the behavior of normal collection but also through datatale. Although I may not be able to avoid this ability DataRow.Delete. So far it seems that I need to do this to remove one or more rows from a table conditionally.

  int max = someDataTable.Rows.Count- 1; (Int i = max; i> = 0; -i) {if (int) someDataTable.Rows [i] .im [0] == some value) {someDataTable.Rows [i]. BeginEdit (); SomeDataTable.Rows [i] .elete (); } And break; } SomeDataTable.AcceptChanges ();  

But I'm not happy with this code. Neither do I believe that I'm missing something. If I have to remove one or more rows conditionally, should I really be forced to hit the row collection sequentially?

(Do not count against it. I am removing the data from the end.)

You can query the dataset and then loosen the selected rows to delete them.

  var rows = DT Selection ("call1"; 5); Prefix (different rows in rows) line Remove ();  

... and you can also create some extension methods to make it easier ...

  myTable.Delete ("col1" 5 "); Delete public static dataplates (this datable table, string filter) {Table. Select (filter). Remove (); Return table; } Public static zero deletion (this IEnumerable & lt; DataRow & gt; rows) {foreach (different rows in rows) row Remove (); }  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -