我有一个对象列表,我需要将每个属性的值放入数组中,而不必多次迭代列表。
这是当前的代码
我似乎无法弄清楚我是否可以使用Linq执行此操作并且只迭代列表一次。
var numberOfItems = invoiceItemNotes.Count;
var postingRcNum = new decimal [numberOfItems];
var postingLocNum = new decimal[numberOfItems];
var invoiceNum = new decimal[numberOfItems];
var orderItemLineNum = new decimal[numberOfItems];
var seqNum = new decimal[numberOfItems];
var text = new string[numberOfItems];
int index = 0;
foreach (var invoiceItemNote in invoiceItemNotes)
{
postingRcNum[index] = invoiceItemNote.POSTING_RC_NUM;
postingLocNum[index] = invoiceItemNote.POSTING_LOC_NUM;
invoiceNum[index] = invoiceItemNote.INVOICE_NUM;
orderItemLineNum[index] = invoiceItemNote.ORDER_ITEM_LINE_NUM;
seqNum[index] = invoiceItemNote.SEQ_NUM;
text[index] = invoiceItemNote.TEXT;
index++;
}
using (var cmd = connection.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = InvoiceInsertCommands.InsertItemNote;
cmd.BindByName = true;
cmd.UpdatedRowSource = UpdateRowSource.None;
cmd.ArrayBindCount = numberOfItems;
cmd.Parameters.Add(InvoiceItemNoteParameters.PostingRcNum, OracleDbType.Decimal, postingRcNum, ParameterDirection.Input);
cmd.Parameters.Add(InvoiceItemNoteParameters.PostingLocNum, OracleDbType.Decimal, postingLocNum, ParameterDirection.Input);
cmd.Parameters.Add(InvoiceItemNoteParameters.InvoiceNum, OracleDbType.Decimal, invoiceNum, ParameterDirection.Input);
cmd.Parameters.Add(InvoiceItemNoteParameters.OrderItemLineNum, OracleDbType.Decimal, orderItemLineNum, ParameterDirection.Input);
cmd.Parameters.Add(InvoiceItemNoteParameters.SeqNum, OracleDbType.Decimal, seqNum, ParameterDirection.Input);
cmd.Parameters.Add(InvoiceItemNoteParameters.Text, OracleDbType.Varchar2, text, ParameterDirection.Input);
cmd.ExecuteNonQuery();
}
答案 0 :(得分:-1)
简单。
object[] invoiceItemNotes = new object[10];
int index = 0;
var invoiceItemNote = invoiceItemNotes[index];