Tag: 表达式

为什么C#.net中的Expression的Body不能使用int,double或bool类型的属性?

我有一个function: private string GetPropertyName(Expression<Func> f) { if ((f.Body as MemberExpression) != null) { return (f.Body as MemberExpression).Member.Name; } return “”; } 它以这种方式使用: string x1 = GetPropertyName(x => Property1); string x2 = GetPropertyName(x => Property2); string x3 = GetPropertyName(x => Property3); 其中Property1是一个int,Property2是一个字符串,Property3是一个对象…… 仅正确返回类型字符串和对象的Property2和Property3的名称,但Property1的f.Body作为MemberExpression为空… 为什么会这样,我们如何更改代码,以便函数正确返回属性的名称?

重建表达式

我有一个表达式: Expression<Func> myExpression = (myObj, theType) => { myObj.Prop > theType }; 我需要动态地将myExpression重建为Expression<Func>类型的新表达式,并将第一个表达式中的“theType”参数替换为具体值123,如: Expression<Func> myNewExpression = myObj => { myObj.Prop > 123 }; 我怎样才能做到这一点? 布里菲尔

为什么这个带有Expression <Func 的模拟不匹配?

我是模拟的新手,我试图做这个模拟示例: Repository.cs public class Repository : IRepository { public List GetForExpression(Expression<Func> expression ) { … //get person from orm using expression } } PersonService.cs public class PersonService { private IRepository _repository; public PersonService(IRepository repository) { _repository = repository; } public List GetAllPersonsWith18More() { var result = _repository.GetForExpression(x => x.Age > 18); return result; } } […]

在运行时创建动态LINQ查询表达式,转换为参数化SQL查询

我想为IQueryable创建自定义表达式。 示例扩展方法: public static IQueryable GetByIntTest(this IQueryable qSource, Expression<Func> field, int? value) { if (value == null) return qSource; ConstantExpression constantExpression = Expression.Constant(value, typeof(int?)); BinaryExpression binaryExpression = Expression.Equal(field.Body, constantExpression); var predicate = Expression.Lambda<Func>(binaryExpression, field.Parameters); return qSource.Where(predicate); } 它的工作原理,但问题是它转换为非参数化的sql。 例如没有扩展名的代码 int userId = 3; var testUsual = Context.Set().Where(u => u.Id == userId); 转换为下一个sql SELECT [Extent1].[Id] AS […]

无法从一组表达式创建复合表达式<Func >

(对底部的回答) 我正在尝试构建一个结合的系统 Func 委托给ExpressionTree,它允许我传入一个值(在这种情况下为badValue)并在谓词全部返回true并考虑二进制操作时获取bool。 这是我第一次使用Expression / ExpressionTrees,所以请保持温和。 我收到这个错误: ArgumentException:无法调用“System.Boolean”类型的表达式 在这条线上: collectAnswers = Expression.And(isEmpty.Body, Expression.Invoke(… 我已经设置了那条线,因为我需要在所有表达式中共享对value的引用(对吗?)。 我理想的情况是只有一堆 Expression<Func> 我可以将它们与逻辑运算符(和/或/不)一起传递到系统中,并在最后得到一个bool。 希望能够动态构建值必须通过的规则。 在我要走的路上,这有可能吗? 如果没有,那么指引我走正确道路的几条指针将不胜感激。 string badValue = “hello!”; const int minSize = 8; const int maxSize = 30; Expression<Func> stringLengthMax = value => value.Length < maxSize; Expression<Func> stringLengthMin = value => value.Length > minSize; Expression<Func> isEmpty = value […]

在另一个lambda表达式中使用lambda表达式

我需要组合两个lambda表达式,第二个表达式“包含”第一个表达式。 我搜索了很多,但没有找到任何明确的答案…… 我要做的是以下内容:第一个表达式“expression1”作为参数传递给方法,并且仅用于定义第二个lambda必须在哪个字段或属性上运行。 原理上,我正在尝试执行以下操作: // simple field selector : Expression<Func> expression1 = obj => obj.field; // trying to use the field selector in 2nd expression : Expression<Func> e2 = obj => [[Result of expression1]].Equals(“myValue”); 换句话说,我想得到: Expression<Func> e2 = obj => obj.field.Equals(“myValue”); 我需要这样做,因为它并不总是被调用的Equals()方法,而是许多不同的方法。 我试图将expression1编译为Func以便在expression2中调用此Func,但是当我在LinQ中使用它时,我得到一个exception,因为LinQ不支持Invoke节点类型。 所以我的问题是:有没有办法将两个表达式的主体结合起来,请问有多喜欢? 谢谢提前!

表达式<Func >作为对象初始化的属性?

我的表达不是很好,我想改进它们所以我想知道是否有人可以解释我是否有可能在类中创建一个属性,可以在实例化期间给出一个值,如下所示: new Column{ ColumnProperty = p => p.Title}; 或者更好(但我认为我在推动它) new Column {ColumnProperty = p => p.Title}; 有这样的类: public class Column { public Expression<Func> ColumnProperty { get; set; } } 它背后的基本思想是我从一堆像这样的Column对象创建一个Grid。 List productList = GetProductsFromDb(); List columnList = new List(); columnList.Add(new Column {ColumnProperty = p => p.ProductId, Heading = “Product Id”}); columnList.Add(new Column {ColumnProperty = p […]

如何修改Expression <Func >的类型参数?

我有以下实例: Expression<Func> 我希望将其转换为以下实例,因此它可用于在Entity Framework中运行查询: Expression<Func> 这将允许我对任何实现IRequiredDate的Model使用通用过滤查询,例如: // In some repository function: var query = DbContext.Set() .FilterByDateRange(DateTime.Today, DateTime.Today); var query = DbContext.Set() .FilterByDateRange(DateTime.Today, DateTime.Today); var query = DbContext.Set() .FilterByDateRange(DateTime.Today, DateTime.Today); // The general purpose function, can filter for any model implementing IRequiredDate public static IQueryable FilterByDate(IQueryable query, DateTime startDate, DateTime endDate) where TModel : IRequiredDate { […]

需要.Net的公式解释器

我正在寻找一个可以在C#应用程序中使用的公式解释器。 它需要能够解释这样的字符串: max(1+2, 4) * x 我发现编写一个快速的公式解释器(codeproject.com)几乎可以满足我的需求,但它不允许具有多个参数的函数。 我可以添加它的function,但我只是想知道这样的事情是否已经存在。 谢谢

深度> 1的成员访问的表达式树

public class Job { public string Name { get; set; } public int Salary { get; set; } } public class Employee { public string Name { get; set; } public Job Job { get; set; } } 如果我想创建一个成员访问Employee.Name的表达式树,这就是我所做的: var param = Expression.Parameter(type, “x”); var memberAccess = Expression.PropertyOrField(param, memberName); return Expression.Lambda<Func>(memberAccess, param); 对Employee.Job.Salary的成员访问权限相当于什么?