我有一個類,ExpenseItem
和一個基於該類的列表。將查詢結果返回給列表框
我目前正在設計一個帶有組合框的窗體,它允許我選擇特定類型的ExpenseItem
Trip,並在列表框中顯示所有結果。
表單代碼(tripSelect
是組合框和listExpenses
是列表框):
private void LoadExpenseList()
{
tripSelect.Items.Clear();
var dateSorted =
from e in roster
group e by e.Trip into tripGroup
select new { Trip = tripGroup.Key };
foreach (var e in dateSorted)
tripSelect.Items.Add(e.Trip);
}
private void LoadExpenseDetail()
{
listExpenses.Items.Clear();
var dateSorted =
from e in roster
orderby e.Trip
select e;
foreach (var e in dateSorted) ;
}
private void ExpenseRecorderForm_Load(object sender, EventArgs e)
{
}
private void tripSelect_SelectedIndexChanged(object sender, EventArgs e)
{
selectedExpense = (ExpenseItem)roster.ToFind((string)tripSelect.SelectedItem);
listExpenses.Items.Add(selectedExpense);
}
private void listExpenses_SelectedIndexChanged(object sender, EventArgs e)
{
tripTextBox.Text = selectedExpense.Trip;
tripTextBox.Enabled = false;
descriptionTextBox.Text = selectedExpense.Description;
amountTextBox.Text = selectedExpense.Amount.ToString();
paymentMethodTextBox.Text = selectedExpense.PaymentMethod;
dateExpenseTimePicker.Value = selectedExpense.Date;
dateExpenseTimePicker.Enabled = true;
noteTextBox.Text = selectedExpense.Note;
}
感謝。儘管(由於FirstOrDefault()),它仍然只檢索第一個值。 因爲,就像你說的,我無法從IEnumerable中投射。 – user1350264 2012-04-23 05:08:26
所以你想達到什麼目的。使用它的代碼試圖將其轉換爲'ExpenseItem',所以我認爲它需要一個值。返回列表有什麼目的,除非你想要多個匹配。 – aqwert 2012-04-23 05:09:57
我確實想要多個值。該表單允許我選擇行程類型,然後顯示所有關聯的行項目並對其進行編輯。 – user1350264 2012-04-23 05:11:39