简体   繁体   English

创建lambda表达式

[英]create lambda expressions

I'm trying to filter a object list (List) bounded to a grid. 我正在尝试筛选绑定到网格的对象列表(列表)。 Now I want to filter this list according to the user required. 现在,我想根据所需用户过滤此列表。 I have combo box to select the field to filter and text box to enter the value. 我有组合框来选择要过滤的字段,并在文本框中输入值。 From there user can select ItemCode or Cost or any property relevant to Item class. 从那里,用户可以选择ItemCode或Cost或与Item类相关的任何属性。 Then how can I create the lambda expression according to the selected field and entered value. 然后如何根据选定的字段和输入的值创建lambda表达式。 .

One flexible but not very simple option would be to use Dynamic LINQ. 一种灵活但不是很简单的选择是使用动态LINQ。 You can construct the query based of the user selection and even use multiple properties with AND and OR operations and comparison operators, etc and use it to filter results. 您可以根据用户选择构造查询,甚至可以将多个属性与AND和OR操作以及比较运算符等配合使用,并使用它来过滤结果。 Here's ScottGu's post on dynamic LINQ. 这是ScottGu在动态LINQ上的帖子

The other straightforward option would be do have a giant switch case for each property in the class. 另一个简单的选择是为类中的每个属性都设置一个巨大的开关盒。

...
case "ItemCode":
       results = records.Where(i => i.ItemCode == criteriaValue);
       break;
case "Cost":
       results = recotds.Where(i => i.Cost == Convert.ToDouble(criteriaValue));
       break;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM