I have a class, ExpenseItem and a list based off that class.
I’m currently designing a form that has a combo box that would allow me to select a particular type of ExpenseItem Trip, and show all results in a listbox.
Form Code (tripSelect is the combo box and listExpenses is the list box):
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;
}
I don’t really understand by what you mean with this.
As for the error, it says you can’t cast from the
IEnumerabletoExpenseItem. You have to apply it like you final example like so and return the item rather thanIEnumerable. I would just skip theWhereclause and go straight into usingFirstOrDefaultI presume
thisis a custom made collection otherwise the Linq extensions won’t work onthisEDIT
If you really want this..
Then you will need to deal with the list from the caller.