Pergunta

I have to select another column in the same query since when creating a single row i have to add the value in the same row.

//Code

  'Loop through the transactions
            For Each iTransactionid In (From TransactionFieldLinq In transactionFieldsCollection Select TransactionFieldLinq.TransactionId).Distinct

                'Create a new data row
                DataRow = DataTable.NewRow()

                'Set the data row properties
                DataRow("TransactionId") = iTransactionid

                'Loop through the transaction fields collection
                For Each TransactionField In (From TransactionFieldLinq In transactionFieldsCollection Where TransactionFieldLinq.TransactionId = CInt(DataRow("TransactionId")))

                    'Set the data row properties
                    DataRow(CStr(TransactionField.FieldId)) = TransactionField.FieldValue

                Next

                'Add the data row to the data table
                DataTable.Rows.Add(DataRow)

            Next

The transactionFieldCollection has another property called StartDate which is in DateTime format and i have to select the StartDate column also in the same query and bind it to the DataTable like the TransactionId.

How can i achieve that?

Foi útil?

Solução 3

Hope this helps.

//Code

'Loop through the transactions
                For Each iTransactionid In (From TransactionFieldLinq In transactionFieldsCollection Select TransactionFieldLinq.TransactionId).Distinct

                    'Create a new data row
                    DataRow = DataTable.NewRow()

                    'Set the data row properties
                    DataRow("TransactionId") = iTransactionid

                    'Loop through the transactions for start date
                    For Each dtStartDate In (From TransactionFieldLinq In transactionFieldsCollection Where TransactionFieldLinq.TransactionId = iTransactionid And TransactionFieldLinq.StartDate <> CDate("12:00:00 AM") Select TransactionFieldLinq.StartDate)

                        'Set the data row properties
                        DataRow("StartDate") = dtStartDate

                    Next
                    'Loop through the transaction fields collection
                    For Each TransactionField In (From TransactionFieldLinq In transactionFieldsCollection Where TransactionFieldLinq.TransactionId = CInt(DataRow("TransactionId")))

                        'Set the data row properties
                        If (TransactionField.FieldId <> 0) Then
                            DataRow(CStr(TransactionField.FieldId)) = TransactionField.FieldValue
                        End If
                    Next

                    'Add the data row to the data table
                    DataTable.Rows.Add(DataRow)

                Next

Outras dicas

You need an anonymous type.

(From TransactionFieldLinq In transactionFieldsCollection 
 Select TransactionFieldLinq.TransactionId, 
 TransactionFieldLinq.StartDate).Distinct
select new {TransactionFieldLinq.TransactionId, 
 TransactionFieldLinq.StartDate}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top