Tag: 表达式

如何在lambda表达式中按字符串属性名进行选择查询?

我想使用lambda select进行查询, 如下所示: public class Foo{ public int Id {get;set;} public string Name {get;set;} public string Surname {get;set;} } var list = new List(); var temp = list.Select(x=> x(“Name”),(“Surname”)); 属性名称需要作为字符串发送,我不知道如何使用,我已经将它作为一个例子。 可能吗? 编辑: Foo list : 1 AB 2 CD 3 EF 4 GH 我不知道通用列表的类型,我有属性名称,如“姓名”,“姓” 我希望如下: Result : AB CD EF GH

有没有办法捕获lambda表达式,以便它不是编译时强制将身份作为表达式或委托类型?

假设我有一个复杂的lambda表达式,如下所示: x => xAHasValue || (xBHasValue && xC == q) || (!xCHasValue && !xAHasValue) || //…expression goes on 我想将它用作Expression<Func in(例如Linq-To-Entities) Queryable.Where方法。 我也想在Enumerable.Where方法中使用它,但Where方法只接受Func ,而不是Expression<Func 。 lambda语法本身可用于生成Expression<Func>或Func (或任何委托类型),但在此上下文中,它不能一次生成多个。 例如,我可以写: public Expression<Func> PairMatchesExpression() { return x => xA == xB; } 就像我写的那样容易: public Func PairMatchesDelegate() { return x => xA == xB; } 问题是我无法以两种方式使用相同的lambda表达式(即x => xA == xB),而无需将其物理地复制到具有两种不同返回类型的两个单独方法中,尽管编译器能够将其编译为任意一个。 […]

Lambda表达式访问对象的属性,该对象是c#中另一个对象的属性

我有这两个class: public class Contratos { //… public int EntidadeFinanceiraId { get; set; } [Column(“Nome”)] public EntidadesFinanceiras entidadeFinanceira { get; set; } //… } public class EntidadesFinanceiras { [Key] public int ID { get; set; } public string Nome { get; set; } //… } 并希望以Contratos.entidadeFinanceira.Nome为基础过滤Contratos列表。 这是根据用户选择的属性过滤列表的方法的一部分。 public IQueryable applyLambdaFilter(string val, string col, string oper, IQueryable […]

reflection大师:为什么我的MethodInfo对象不相等?

基本上,在静态System.Linq.Expressions.Expression.Bind()方法中发生的一些内部检查在我的属性上显示“方法不是属性访问器”,显然是属性。 使用Reflector,我已经最小化了导致问题的代码量,而且我不能为我的生活找出为什么会发生这种情况。 我唯一的猜测是,它与属性不属于类本身的事实有关,但我认为这应该仍然有效: 我试图用尽可能少的代码做一个小例子。 下面的代码应该完整运行。 using System; using System.Reflection; public class Base { public virtual int Id { get; set; } } // As you can see, SubClass does not override the Id property of Base. public class SubClass : Base { } class Program { static void Main(string[] args) { // Getting the property […]

是否可以在Lambda表达式中包含SqlFunctions.StringConvert?

我一直在学习表达式并使用下面的代码为数据库模型添加构建表达式(EF4 – ORACLE而不是SQL!) 这完全适用于Oracle,并允许我动态构建谓词,如”CustomerId”, “Contains”, 2到f=>f.CustomerId.ToString().ToLower().Contains(“2”) 但是,如果我尝试对抗SQL Server,那么它会失败,因为我需要调用SqlFunctions.StringConvert – 但我不知道如何将它包含在Lambda中? 我的最终结果将是: f=> SqlFunctions.StringConvert(f.CustomerId).ToLower().Contains(“2”) 谢谢 :) 编辑:添加了我尝试过的例子 这段代码看起来几乎可以工作,有点像! 但是,它会在var sqlExpression行上引发错误 Expression of type ‘System.Double’ cannot be used for parameter of type ‘System.Nullable`1[System.Double]’ of method ‘System.String StringConvert(System.Nullable`1[System.Double])’ MethodInfo convertDouble = typeof(Convert).GetMethod(“ToDouble”,new Type[]{typeof(int)}); var cExp = Expression.Call(convertDouble, left.Body); var entityParam = Expression.Parameter(typeof(TModel), “f”); MethodInfo sqlFunc = typeof(SqlFunctions).GetMethod(“StringConvert”, new […]

创建使用动态生成类型的表达式树

我有一个完全初始化的MethodBuilder和EnumBuilder 。 MethodBuilder指向动态程序集的入口点。 它有以下签名: public static int Main (string [] args) {…} 程序集生成代码工作正常,我可以使用Reflection.Emit来测试它。 我想要从表达式树中保存目标代码,而不是发出IL。 这应该: 声明一个枚举变量 为它赋值 写入控制台 读取暂停控制台 将枚举值作为Int32返回 表达树: // Intention: Declare string [] args in the expression scope. var arguments = Expression.Parameter(typeof(string []), “args”); // Intention: var code = default(MyDynamicEnum); var code = Expression.Variable(builderEnum, “code”); // Intention: code = MyDynamicEnum.Two; var assign […]

使用表达式在NHibernate QueryOver中进行动态排序

给出以下QueryOver : UserProfile userProfileAlias = null; Pegfile pegfileAlias = null; var q = Session.QueryOver(() => pegfileAlias) .JoinAlias(() => pegfileAlias.UserProfile, () => userProfileAlias); 我想通过交换使以下语句动态化 q = q.OrderBy(() => userProfileAlias.Forename).Asc; (OrderBy(Expression<Func>或( OrderBy(Expression<Func>) ) 同 q = q.OrderBy(GetMemberExpression(userProfileAlias, “Forename”)).Asc; 我借了另一篇文章来获取 private Expression<Func> GetMemberExpression(UserProfile instance, string propertyName) { var arg = Expression.Constant(instance, typeof(UserProfile)); var body = Expression.Convert(Expression.PropertyOrField(arg, propertyName), typeof(UserProfile)); […]

从给定的Type创建表达式<Func >

我希望通过在代码中构建表达式动态地使用CsvHelper,代码表示给定类型的属性成员访问。 我试图传递这些表达式的方法具有以下签名: public virtual CsvPropertyMap Map( Expression<Func> expression ) { // } 因此,您通常会调用它,对于您要映射的任何给定类型,如下所示(对于具有名为’stringProperty’的属性的类型): mapper.Map(x => x.StringProperty); 传入lambda,内部转换为Expression<Func> 我试图使用表达式在代码中创建此表达式。 在编译时它一切正常(因为它返回一个Expression<Func> ),但在运行时我得到一个exception’不是成员访问’。 这是一个代码,它接受一个PropertyInfo对象来表示我想要映射的属性: private Expression<Func> CreateGetterExpression( PropertyInfo propertyInfo ) { var getter = propertyInfo.GetGetMethod(); Expression<Func> expression = m => getter.Invoke( m, new object[] { } ); return expression; } 基本上,我如何在代码中正确构建表达式?

如何在运行时计算表达式参数?

我需要解决的问题是通过IF语句运行数据。 IF语句由运行时的SQL表生成。 我已经设法通过使用表达式和Lambda表达式来完成此操作。 我的表有memberName; 操作; 目标。 所以例如我从表中得到“Age”,“GreaterThan”和“40”,然后我编译它。 var rule = new Rule(rowRule[“memberName”].ToString(), rowRule[“Operator”].ToString(), rowRule[“Target”].ToString()); Func compiledRule = CompileRule(rule); 我得到一个真或假的价值,它完美无缺。 public static Func CompileRule(Rule r) { var paramUser = Expression.Parameter(typeof(User)); Expression expr = BuildExpr(r, paramUser); // build a lambda function User->bool and compile it return Expression.Lambda<Func>(expr, paramUser).Compile(); } static Expression BuildExpr(Rule r, ParameterExpression param) { var […]

如何将表达式从类型接口转换为特定类型

在我的界面中,我有以下定义 List GetListOfFoo(Expression<Func> predicate) where T : IFoo; 在我的实现中,我将以特定类型转换表达式: if (typeof(T) == typeof(Foo)) { Expression converted = Expression.Convert(predicate.Body, typeof(Foo)); Expression<Func> newPredicate = Expression.Lambda<Func>(converted, predicate.Parameters); } 我尝试使用这样的实现: Expression<Func> predicate = c => c.Name == “Myname”; _repository.GetListOfFoo(predicate); 我没有编译错误,但是如果我使用它,我会得到一个exception,即ExpressionBody中定义的bool参数。 我的问题在哪里?