WPF Linq一个包含或排除项目的集合
我不太确定标题的字眼.这是我正在寻找的是否可能在linq语句中.
I'm not really sure how to word the title. This is what I'm looking for if it's possible in a linq statement.
我有3个组合框,用户可以选择一个值.在每个组合框的SelectionChanged事件上,将过滤出一个observablecollection,它将仅根据值填充另一个collection.组合框的顶部包含一个空白,用户可以选择不在该列上应用过滤器.
I have 3 comboboxes that the user can select a value. On the SelectionChanged event of each combobox will filter out an observablecollection that would only populate another collection based on the values. The comboboxes contains a blank at the top for the user to select not to apply the filter on that column.
有没有一种简单的方法可以使用linq来做到这一点?
Is there a simple way to use linq to do this?
希望我已经解释清楚了.
Hope I explained that clear enough.
您的xaml可能是这样的:
You xaml might be like this:
<ComboBox x:Name="cmb1" SelectionChanged="Selector_OnSelectionChanged">
<ComboBox x:Name="cmb2" SelectionChanged="Selector_OnSelectionChanged">
<ComboBox x:Name="cmb3" SelectionChanged="Selector_OnSelectionChanged">
处理程序的代码:
var someCollection = new List<int>(); // some your collection
// stubs
Expression<Func<int, bool>> predicate1 = (x) => true;
Expression<Func<int, bool>> predicate2 = (x) => true;
Expression<Func<int, bool>> predicate3 = (x) => true;
// real predicates
if (cmb1.SelectedIndex >= 0)
predicate1 = (x) => x == (int)cmb1.SelectedValue;
if (cmb2.SelectedIndex >= 0)
predicate2 = (x) => x == (int)cmb2.SelectedValue;
if (cmb3.SelectedIndex >= 0)
predicate3 = (x) => x == (int)cmb3.SelectedValue;
// eval and out
lstBox.Items = someCollection.Where(predicate1)
.Where(predicate2)
.Where(predicate3)
.ToList();
我写了这个细节,以使其更加清晰.
I wrote this deatailed in order to make more clear.