将数学运算符存储在变量中

时间:2018-11-26 10:40:15

标签: c# linq mvvm collectionview

我正在C#MVVM中创建一个应用程序。我有一个简单的问题。有可能将数学运算符存储在变量中吗?我有这样的代码:

public ICollectionView FilteredCollection
            {
                get
                {
                    return filteredCollection;
                }
                set
                {
                    filteredCollection = value;
                    OnPropertyChanged("FilteredCollection");
                }
            }



 FilteredCollection.Filter = x => (
                    (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName))
                    && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID)
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || ((Employee)x).EmployeeSalary == Convert.ToInt32(DynamicSearchEmployeeSalary))
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner))
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID))
                    );

我想要实现的目标: 在第四行(DynamicSearchEmployeeSalary)中,数学运算符应取决于以下条件:

if (IsPressedEqual == true)
VARIABLE = "=="
if (IsPressedLess == true)
VARIABLE = "<"
if (IsPressedGreater == true)
VARIABLE = ">"
if (IsPressedLess == true && IsPressedEqual == true)
VARIABLE = "<="
if (IsPressedGreater == true && IsPressedEqual == true)
VARIABLE = ">="

场景:

例如,我在文本框中输入了10000之类的值,然后单击带有“ =”运算符的按钮。因此,我希望收到薪水等于10000的员工。 然后,单击“>”。而且我的员工的薪水更高,等于10000。

enter image description here

 FilteredCollection.Filter = x => (
                        (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName))
                        && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID)
                        && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || ((Employee)x).EmployeeSalary VARIABLE Convert.ToInt32(DynamicSearchEmployeeSalary))
                        && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner))
                        && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID))

1 个答案:

答案 0 :(得分:1)

我已经制作了一个简单的基于规则的引擎...我认为它将帮助您解决问题...

请在以下位置找到它作为nuget包:https://www.nuget.org/packages/IbnSherien.RuleBasedEngine/

您可以创建如下规则:

var rule = RuleEngine.CreateRule<Employee>()
                     .If<Employee>(e => e.EmployeeSalary).GreaterThan(DynamicSearchEmployeeSalary)
                     .Validate();

FilteredCollection.Filter = x => (
                    (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName))
                    && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID)
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || rule.Match((Employee)x).IsMatch)
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner))
                    && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID))

可以随意添加任何评论或对软件包做出贡献