.net 从foreach中的LINQ中选择2个值
我必须在同一查询中选择另一列,因为在创建单行时,我必须在同一行中添加值 //代码.net 从foreach中的LINQ中选择2个值,.net,vb.net,linq,.net,Vb.net,Linq,我必须在同一查询中选择另一列,因为在创建单行时,我必须在同一行中添加值 //代码 'Loop through the transactions For Each iTransactionid In (From TransactionFieldLinq In transactionFieldsCollection Select TransactionFieldLinq.TransactionId).Distinct 'Create a
'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
'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
transactionFieldCollection
有另一个名为StartDate
的属性,它是DateTime
格式,我必须在同一查询中选择StartDate
列,并将其绑定到DataTable
,就像TransactionId一样
我如何才能做到这一点?您需要一个匿名类型
(From TransactionFieldLinq In transactionFieldsCollection
Select TransactionFieldLinq.TransactionId,
TransactionFieldLinq.StartDate).Distinct
希望这有帮助
//代码
'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
'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
“在事务中循环
对于中的每个iTransactionid(从transactionFieldsCollection中的TransactionFieldLinq选择TransactionFieldLinq.TransactionId)。不同
'创建新的数据行
DataRow=DataTable.NewRow()
'设置数据行属性
DataRow(“TransactionId”)=iTransactionid
'循环查看开始日期的事务
对于中的每个dtStartDate(从TransactionFieldLinq中的transactionFieldsCollection,其中TransactionFieldLinq.TransactionId=iTransactionid和TransactionFieldLinq.StartDate CDate(“12:00:00 AM”)选择TransactionFieldLinq.StartDate)
'设置数据行属性
DataRow(“StartDate”)=dtStartDate
下一个
'在事务字段集合中循环
对于中的每个TransactionField(来自transactionFieldsCollection中的TransactionFieldLinq,其中TransactionFieldLinq.TransactionId=CInt(数据行(“TransactionId”))
'设置数据行属性
如果为(TransactionField.FieldId 0),则
DataRow(CStr(TransactionField.FieldId))=TransactionField.FieldValue
如果结束
下一个
'将数据行添加到数据表中
DataTable.Rows.Add(DataRow)
下一个
我已经试过了,但我无法完成全部。你能发布一个完整的吗?