Home > Blockchain >  VB.net LINQ select specific rows from datatable
VB.net LINQ select specific rows from datatable

Time:12-11

I have s situation where i load all my data in a DataTable, but i need to use only the columns which don't start with a prefix ("#") in my case. I could build a function to go through the data, but i wanted to use LINQ as i'm in the process of learning it.

What i have so far:

Dim l = (From r In (dt.AsEnumerable())
                     From c As DataColumn
                             In r.Table.Columns
                     Where Not c.ColumnName.Contains("#")).ToList()

But the result is not what i need. I realize what's wrong with the function but i just can't figure out how to write it

To make it more clear:

DataTable:
      [Column1][Column2][Column3]
[Row1]    123      abv   12/10/21
[Row2]    555     sfsf   12/12/21

I don't need [Column3] and, because of the specific of what i'm trying to do, i need to use the complete DataTable. So, the result would be:

DataTable, or List(of) or IEnumerable..:
      [Column1][Column2]
[Row1]    123      abv   
[Row2]    555     sfsf   

And i was hoping to use LINQ, but if it's to complicated, or not possible, i will use the normal vb way. i'll build a new DataTable with only the desired columns...

CodePudding user response:

Not 100% sure what you are trying to achieve, but I think you want to do something like this:

Dim result = (From c In datatable.Columns Where Not c.ToString.StartsWith("#"c)).ToList

CodePudding user response:

This is what i ended up with. I just remove the column i don't need from the collection.

dt.Columns.Remove((From c As DataColumn In dt.Columns Where c.ColumnName.StartsWith("#")).First)
  • Related