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 ) ) );
}