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...
You can do a Select if you know which columns you want to keep
dim result = dt.AsEnumerable().[Select](Function(x) New With {
Key .Col1 = x.Field(Of String)("Column1"),
Key .Col2 = x.Field(Of String)("Column2")
}).Tolist()
Or else you can do
dim result = dt.AsEnumerable().[Select](Function(x) New With {
Key .Col1 = x.Field(Of String)("Column1"),
Key .Col2 = x.Field(Of String)("Column2"),
'put every field.....
}).Where(Function(y) Not y.Col3.StartsWith("#"c)).Tolist()