这里有三个要素。
- 带有一堆方法的旅行费用清单
- Windows 窗体(tripChoose 是一个组合框,listExpenses 是一个列表框)
- 对象本身(旅行费用)
我只想将 e.Trip 传递给“ToFind”方法。相反,整个字符串被传递。我只需要以某种方式解析出 e.Trip。有任何想法吗?我仍然希望在组合框中同时显示金额和行程,但我只需要 e.Trip 即可传递给该方法。见下文:
在表格中,我有这个
private void LoadExpenseListSums()
{
expenseTotalSelect.Items.Clear();
var dateSorted =
from e in roster
group e by e.Trip into tripGroup
select new { Trip = tripGroup.Key, SumAmount = tripGroup.Sum(e => e.Amount) };
foreach (var e in dateSorted)
tripChoose.Items.Add(string.Format("{0} | ${1}", e.Trip, e.SumAmount));
}
private void tripChoose_SelectedIndexChanged(object sender, EventArgs e)
{
listExpenses.Items.Clear();
tripTextBox.Clear();
descriptionTextBox.Clear();
amountTextBox.Clear();
paymentMethodTextBox.Clear();
noteTextBox.Clear();
IEnumerable<TripExpense> selectedExpenses = roster.ToFind((string)tripChoose.SelectedItem);
foreach (TripExpense item in selectedExpenses)
listExpenses.Items.Add(item);
}
LoaExpenseListSums 发生在加载表单时,我希望 e.Triproster.ToFind((string)tripChoose.SelectedItem);
成为传递给此方法的唯一部分
public IEnumerable<TripExpense> ToFind(string trip)
{
return this.Where(e => e.Trip == trip);
}
如果 LoadExpenseListSums 被这样重写,它工作正常:
private void LoadExpenseListSums()
{
expenseTotalSelect.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)
tripChoose.Items.Add(e.Trip);
}