WPF Linq一个包含或排除项目的集合 [英] WPF Linq a collection to include or exclude items

查看:75
本文介绍了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.

这篇关于WPF Linq一个包含或排除项目的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆