在选择组合框时将所有匹配值返回到列表框

时间:2012-04-22 22:52:40

标签: c# wpf

我有一张带有ListBox和一堆TextBox的表单用于编辑记录。我还有一个ComboBox来选择trip的类型(这是在表单中定义的)。

    private void LoadExpenseList()
        {
            tripSelect.Items.Clear();
            var dateSorted =
                from e in roster
                orderby e.Trip
                select e;
            foreach (var e in dateSorted)
                tripSelect.Items.Add(e.Trip);
        }

private void tripSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedExpense = (ExpenseItem)roster.TripFind((string)tripSelect.SelectedItem);
            listExpenses.Items.Add(selectedExpense);
        }


 private void listExpenses_SelectedIndexChanged(object sender, EventArgs e)
        {}

现在,当我选择trip时,我只获得传递给ListBox的第一个结果,这就是为什么(这是在列表类中定义的)

public ExpenseItem TripFind(string trip)
    {
        var specificExpenseItem =
           from e in this
           where e.Trip == trip
           select e;
        if (specificExpenseItem.Count() >= 1)
            return specificExpenseItem.First();
        return null;
    }

每次尝试重写时我都会遇到问题!我得到not all paths return a value或者JIT调试器告诉我我无法通过它。

这是我尝试过的最后一件事:

public ExpenseItem TripFind(string trip)
    {
        var specificExpenseItem =
           from e in this
           where e.Trip == trip
           select e;
        foreach (var e in specificExpenseItem);
        return null;
    }

任何帮助?

1 个答案:

答案 0 :(得分:0)

您可以用

替换所有内容
return this.FirstOrDefault(e => e.Trip == trip);
相关问题