Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向谓词列表添加多个条件_C#_Wpf_Predicate - Fatal编程技术网

C# 向谓词列表添加多个条件

C# 向谓词列表添加多个条件,c#,wpf,predicate,C#,Wpf,Predicate,我有公司,我正试图使用标准对其进行筛选。每个公司都有一个当前状态,每次用户选中复选框定义筛选器时,都会调用筛选方法。除了一件事之外,我现在的工作方式几乎完全符合我的要求。这就是我所拥有的 private void FilterCompanyType(object sender, RoutedEventArgs e) { criteria.Clear(); if (currentCheckBox.IsChecked == true)

我有
公司
,我正试图使用
标准对其进行筛选。每个
公司
都有一个
当前状态
,每次用户选中
复选框
定义筛选器时,都会调用筛选方法。除了一件事之外,我现在的工作方式几乎完全符合我的要求。这就是我所拥有的

    private void FilterCompanyType(object sender, RoutedEventArgs e)
    {
        criteria.Clear();

        if (currentCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 1));
        }

        if (nonCurrentCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 0));
        }

        foreach (CheckBox checkBox in companyFilters.Children)
        {
            if (!CheckCheckBoxes())
            {
                dataGrid.ItemsSource = null;
                compDetailsLabel.Content = string.Empty;
            }
            else
            {
                dataGrid.ItemsSource = CompanyICollectionView;
                CompanyICollectionView.Filter = dynamic_Filter;
                SetSelectedCompany(selectedIndex);
                dataGrid.SelectedIndex = 0;
            }
        }
    }

这只返回一个空的
DataGrid
。如何更改
谓词
以同时考虑两者?

如果我清楚地理解,
If
语句应该是这样的

if ( currentCheckBox.IsChecked == true && nonCurrentCheckBox.IsChecked == false )
{
    criteria.Add( new Predicate<CompanyModel>( x => x.CurrentStatus == 1 ) );
}
else if ( nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == false )
{
    criteria.Add( new Predicate<CompanyModel>( x => x.CurrentStatus == 0 ) );
}
else if ( nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == true )
{
    criteria.Add( new Predicate<CompanyModel>( x => ( x.CurrentStatus == 0 || x.CurrentStatus == 1 ) ) );
}
if(currentCheckBox.IsChecked==true&&nonCurrentCheckBox.IsChecked==false)
{
Add(新谓词(x=>x.CurrentStatus==1));
}
else if(nonCurrentCheckBox.IsChecked==true&¤tCheckBox.IsChecked==false)
{
Add(新谓词(x=>x.CurrentStatus==0));
}
else if(nonCurrentCheckBox.IsChecked==true&¤tCheckBox.IsChecked==true)
{
添加(新谓词(x=>(x.CurrentStatus==0 | | x.CurrentStatus==1));
}

您可以尝试
条件。添加(新谓词(x=>(x.CurrentStatus==1 | | x.CurrentStatus==0))
,因为两者都被选中。@bars222不幸的是,使用此选项仍然不会导致
数据网格中显示任何
公司
,您还应该将代码添加到以前的
if
语句中。if(currentCheckBox.IsChecked==true&&nonCurrentCheckBox.IsChecked==false)
if ( currentCheckBox.IsChecked == true && nonCurrentCheckBox.IsChecked == false )
{
    criteria.Add( new Predicate<CompanyModel>( x => x.CurrentStatus == 1 ) );
}
else if ( nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == false )
{
    criteria.Add( new Predicate<CompanyModel>( x => x.CurrentStatus == 0 ) );
}
else if ( nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == true )
{
    criteria.Add( new Predicate<CompanyModel>( x => ( x.CurrentStatus == 0 || x.CurrentStatus == 1 ) ) );
}