Change the sequens of columns in datatable with linq - vb.net

I have a Datatable with the columns
Name|Age|Job
I query that Datatable with Linq and do some manipulations.
Dim query = From row in dt.AsEnumerable.where(...)
The returned object is a datatable that I need for further procedures.
But now I have to change the column sequence to
Age|Name|Job
and do not know how to do that.
I tried:
Dim query1 = From column In query
Select column.Age, column.Name, column.Job
But query1 is not a Datatable object anymore. Could anyone help me get the resorting of the columns done that I still have a datatable object?

You can use DataColumn.SetOrdinal() method to reorder columns. Assuming that query is a datatable with columns Name|Age|Job, the following will reorder the columns to Age|Name|Job :
query.Columns("Age").SetOrdinal(0)
query.Columns("Name").SetOrdinal(1)

Related

How do you query a table filtering on a substring of one of the columns?

I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')

Comparing field names of two tables

I have spent a few hours searching for a similar Problem to mine. though there are a few replies, i could not find any to the Point.
so basically, i have two tables. one is a Standard Access table (table1) and the other is a table imported from Excel (table2). now table2 has the field names of table 1 and also a few other extra fields that i dont Need. now i want Access to compare These two tables and copy into table 1 the contents of whichever fields that it can match from table 2.
i have tried INFORMATION_SCHEMA.COLUMNS, but the code does not seem to recognise this Expression.
can anyone suggest some ideas please!!! can give more Details if required.
Thanks a lot..
saran
Something like this would give you the field names.. If static field names, id suggest importing to a table, derrived from your current import, and delete and append rather importing each time, and use a join as suggested.
Dim a As ADODB.Connection
Set a = New ADODB.Connection
Dim r As ADODB.Recordset
Set a = CurrentProject.Connection
Set r = a.OpenSchema(adSchemaColumns)
r.Filter = "[TABLE_NAME]='Your table name'"
or another way
r.open "Select * from table",a,1
loop r.fields

Disable datagridview automatic ordering VB.NET

In some cases (I believe when inner join is used) datagridview does not represent correctly the column order populated from SQL query.
For example, I query the table columns in this order:
Select Booking_ID, Client_Name, Room_ID
although SQL return columns in correct order, however datagridview displays columns automatically in different order:
Room_ID || Booking_ID || Client_Name
The question is, how to disable this automatic column horizontal ordering, so datagridview displays the table exactly how SQL does?
I am using datasource method to fill the datagrid.
SQLDA = New SqlDataAdapter(SqlCmd)
SQLDataset = New DataSet
SQLDA.Fill(SQLDataset)
Form2.DGVData.DataSource = SQLDataset.Tables(0)
In most cases this problem is not present, and datagridview shows the table exactly as SQL does.
Thank you in advance.
PS: I would post the images but I'm not allowed since 10 reputation is required.
Simple, but should work:
dgvDataGridView.Columns("Booking_ID").DisplayIndex = 0
dgvDataGridView.Columns("Client_Name").DisplayIndex = 1
dgvDataGridView.Columns("Room_ID").DisplayIndex = 2
'...

Load MAX value from sql table into a DataTable

I need to reed aggregated data from SQL into .Net, to do this I use a DataAdapter to Fill a DataTable. The query looks something like this:
SELECT ACTNUMBR AS [SegmentNumber], MAX(DSCRIPTN) AS [Description], COUNT(*) AS [UsageCount]
The SegementNumber and the UsageCount values load into the DataTable as expected, but I cannot get the Description column to load values, it remains blank (running the query in SQL does return values). If I hardcode a value then it loads the values into the DataTable but as soon as I use a MAX() expression it does not load.
I have tried specifying the column detail for the DataTable but it had no effect.
Is this known behavior? How do I fix / work around it?
I believe the problem is in your SQL. You can't include an aggregate function (MAX) without doing a group BY.
SELECT ACTNUMBR AS [SegmentNumber],
MAX(DSCRIPTN) AS [Description],
COUNT(*) AS [UsageCount]
FROM SOMETABLE
GROUP BY ACTNUMBR

I want to go to last record of access database without using ID

I have been trying to find this. I am using access 2010 and I have some data in a few tables and I want to select the last row from each one and add them to a new database. All the databases have random ID so I can't use the Sort by ID function.
If the table is small, you can pass it to a datatable in the frontend and use something like this,
lastRow = datatable.rows(datatable.rows.count-1)
Else, You can add a 'created_datetime' field in database which holds the inserted datetime and retrieve its maximum date since your ID field has random number...
open vba (alt + F11)
dim rst as recordset
set rst = docmd.runsql (sql statement here) e.g. (select * from tablename)
rst.movelast
you've gotten to your last record in vba
you can add it to a new table database by using an insert statement.