从所选字段创建lambda表达式 [英] Creating lambda expressions from the selected field

查看:68
本文介绍了从所选字段创建lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤绑定到网格的对象列表(List< item>).现在,我要根据所需的用户筛选此列表.
我有组合框来选择要过滤的字段,并在文本框中输入值.从那里,用户可以选择ItemCode或Cost或与Item类相关的任何属性.然后,如何根据所选字段和输入的值创建lambda表达式.

解决方案

做一个开关案例,并根据所选值定义lambda表达式. br/>

 // 注意:更新类型(您要获取的示例格式)
 var 结果= 列表< classnamehere>();
 var  filter = txtbox.Text;

开关(ddl.SelectedValue){
   案例 " :
      // 进行过滤
      结果= listSource.Find(s = >  {s.cost == filter});
       break ;
   案例 " :
      结果= listSource.Find(s = >  {s.itemcode == filter});
       break ;
   默认:
      结果= listSource;
       break ;
} </ 此处的类名 >  



如果我错了,请指正我,但我认为您不能根据您的情况动态构造lambda exp.

祝你好运!


像这样尝试

这是您要对其应用过滤的类


公共类ClassName
{
公共字符串ItemCode {get;放; }
公共字符串Cost {get;放; }
公共重写字符串ToString()
{
返回"ItemCode" + ItemCode +,Cost" + Cost;
}
}

这是应用过滤的方法

列表< ClassName> getFiltered(List< ClassName> className,Func< ClassName,bool> func)
{
返回className.Where(c => func(c)).ToList();
}

如果您注意到我们传递的谓词"func"以参数"ClassName"作为输入,返回类型为bool.

列表< ClassName> i =新列表< ClassName>();
i.Add(新的ClassName {ItemCode ="1",Cost ="1"});
i.Add(新的ClassName {ItemCode ="2",Cost ="2"});
i.Add(新的ClassName {ItemCode ="3",Cost ="3"});
i.Add(新的ClassName {ItemCode ="4",Cost ="4"});

列表< ClassName> ClassName = getFiltered(i,t => t.Cost == txtFilterBox.Text);

现在查看第二个参数"t => t.Cost == txtFilterBox.Text",该参数接受类型为"ClassName"的参数,返回类型为bool.



foreach(ClassName中的ClassName im)
Response.Write(im.ToString());


我想尝试一下,它应该对您有用


参考表达式树 [^ ]

 字符串 filterText = " ;
字符串 fieldName = " ;
 var  type =  typeof (YourClass);
ParameterExpression param = Expression.Parameter(type," );
 var  field = type.GetProperty(fieldName);
MemberExpression成员= Expression.MakeMemberAccess(param,field);
ConstantExpression筛选器= Expression.Constant(filterText);
BinaryExpression比较= Expression.Equal(成员,过滤器);
表达式< Func< YourClass,bool> lambdaExpression =
    Expression.Lambda< Func< YourClass,bool>>(compare,param); 


这应该做到.


I''m trying to filter a object list (List<item>) 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. Then how can I create the lambda expression according to the selected field and entered value.

解决方案

Do a switch case and define your lambda expression based on your selected value.


//note: update the type (sample format you want to get)
var result = new List<classnamehere>();
var filter = txtbox.Text;

switch (ddl.SelectedValue) {
   case "cost":
      //do filtering
      result = listSource.Find(s=>{s.cost == filter});
      break;
   case "itemcode":
      result = listSource.Find(s=>{s.itemcode == filter});
      break;
   default:
      result = listSource;
      break;
}</classnamehere>



Correct me if I''m wrong but I think you CANT dynamically construct lambda exp on your scenario.

Good luck!


Try like this

This is the class on which you want to apply the filtering


public class ClassName
{
public string ItemCode { get; set; }
public string Cost { get; set; }
public override string ToString()
{
return "ItemCode " + ItemCode + ", Cost " + Cost;
}
}

This is the method to apply filtering

List<ClassName> getFiltered(List<ClassName> className, Func<ClassName, bool> func)
{
return className.Where(c => func(c)).ToList();
}

If you notice that we are passing a predicate named "func" which takes a parameter "ClassName" as input and return type is bool.

List<ClassName> i = new List<ClassName>();
i.Add(new ClassName { ItemCode = "1", Cost = "1" });
i.Add(new ClassName { ItemCode = "2", Cost = "2" });
i.Add(new ClassName { ItemCode = "3", Cost = "3" });
i.Add(new ClassName { ItemCode = "4", Cost = "4" });

List<ClassName> ClassName = getFiltered(i, t => t.Cost == txtFilterBox.Text);

Now see the second parameter "t => t.Cost == txtFilterBox.Text" which is accepting a parameter of type "ClassName" and return type is bool.



foreach (ClassName im in ClassName)
Response.Write(im.ToString());


I Think try with this, it should work for you


Reference Expression Trees[^]

string filterText = "Hello";
string fieldName = "FieldName";
var type = typeof(YourClass);
ParameterExpression param = Expression.Parameter(type, "s");
var field = type.GetProperty(fieldName);
MemberExpression member = Expression.MakeMemberAccess(param,field);
ConstantExpression filter = Expression.Constant(filterText);
BinaryExpression compare = Expression.Equal(member, filter);
Expression<Func<YourClass, bool>> lambdaExpression =
    Expression.Lambda<Func<YourClass, bool>>(compare,param);


This should do it.


这篇关于从所选字段创建lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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