Tag: expression trees

如何使用EF查询中的函数参数化选择器?

我有一个投影函数,我传递给IQueryable.Select()方法: private static Expression<Func> GetPriceSelector(){ return e => new PriceItem { Id = e.Id, Price = Math.Round(e.Price, 4) }; } 一切正常,但我想像这样参数化: private static Expression<Func> GetPriceSelector(Func formula){ return e => new PriceItem { Id = e.Id, Price = formula(e) }; } 所以我可以称之为 prices.Select(GetPriceSelector(e => Math.Round(e.Price, 4))) 不幸的是,EF抱怨它: LINQ to Entities中不支持LINQ表达式节点类型“Invoke” 如何重写代码让EF快乐?

如何评估ExpressionVisitor中的表达式?

在执行Expression之前,我需要使用ExpressionVisitor来分析它。 根据我的需要,我需要评估Divide表达式的正确部分,但我不知道该怎么做。 这是我的示例代码: internal class RulesChecker : ExpressionVisitor { private readonly object data; public RulesChecker(object data) { this.data = data; } protected override Expression VisitBinary(BinaryExpression node) { if (node.NodeType == ExpressionType.Divide) { var rightExpression = node.Right; // compile the right expression and get his value } return base.VisitBinary(node); } } 假设我有这个代码来评估: Expression<Func> expression = x […]

生成多项式评估的方法

我试图想出一种优雅的方法来处理一些生成的多项式。 以下是我们将(专门)关注此问题的情况: order是生成n阶多项式的参数,其中n:= order + 1。 i是0..n范围内的整数参数 多项式在x_j处有零,其中j = 1..n且j≠i(此时应该清楚StackOverflow需要一个新特性或它存在并且我不知道它) 多项式在x_i处求值为1。 由于此特定代码示例生成x_1 .. x_n,我将解释它们是如何在代码中找到的。 这些点均匀间隔x_j = j * elementSize / order apart,其中n = order + 1 。 我生成一个Func来评估这个多项式¹。 private static Func GeneratePsi(double elementSize, int order, int i) { if (order < 1) throw new ArgumentOutOfRangeException("order", "order must be greater than 0."); if (i order) throw […]

构建表达式树

我正在努力解决如何为更多lambda建立表达式树的想法,例如下面的那个,更不用说可能有多个语句的东西了。 例如: Func GetBytes = x => x.HasValue ? BitConverter.GetBytes(x.Value) : new byte[1] { 0xFF }; 我很感激任何想法。

为什么在表达式树中需要转换

从5分钟前我问过这个问题 ,很明显以下代码会引发exception,说明这一点 未处理的exception:System.InvalidOperationException:没有为类型’System.Nullable`1 [System.Int32]’和’System.Int32’定义二进制运算符Equal。 码 public static void GetResultCollection() { AccrualTrackingEntities db = new AccrualTrackingEntities(); var result = db.CreateQuery(String.Format(“[{0}]”, typeof(T).Name + “s”)); int? ItemTypeValue = 1; var param = Expression.Parameter(typeof(T)); var lambda = Expression.Lambda<Func>( Expression.Equal( Expression.Property(param, “ProcInstId”), Expression.Constant(ItemTypeValue)), param); var list = result.Where(lambda).ToList(); } 但是,此代码与Expression.Constant中明确列出的类型确实有效 class Program { public static void GetResultCollection() { AccrualTrackingEntities db […]

修改IQueryable.Include()的表达式树以向连接添加条件

基本上,我想实现一个存储库,即使通过导航属性也可以过滤所有软删除的记录。 所以我有一个基本实体,类似的东西: public abstract class Entity { public int Id { get; set; } public bool IsDeleted { get; set; } … } 还有一个存储库: public class BaseStore : IStore where TEntity : Entity { protected readonly ApplicationDbContext db; public IQueryable GetAll() { return db.Set().Where(e => !e.IsDeleted) .InterceptWith(new InjectConditionVisitor(entity => !entity.IsDeleted)); } public IQueryable GetAll(Expression<Func> predicate) […]

如何反映T为查询构建表达式树?

我正在尝试构建一个通用类来处理来自EF的实体。 这个类与存储库进行对话,但是这个类创建了发送到存储库的表达式。 无论如何,我只是想实现一个虚拟方法,它将作为常见查询的基础。 具体来说,它将接受一个int ,它只需要对相关实体的主键执行查询。 我一直在搞砸它,我已经建立了一个反映,可能会或可能不会。 我这样说是因为我得到一个带有LINQ to Entities消息的NotSupportedException , 无法识别方法’System.Object GetValue(System.Object,System.Object [])’方法,并且此方法无法转换为商店表达式。 那么我尝试了另一种方法,它产生了相同的exception,但LINQ表达式错误的节点类型’ArrayIndex’在LINQ to Entities中不受支持。 我知道这是因为EF不会像L2S那样解析表达式。 无论如何,我正在跳跃一个有更多经验的人可以指出我正确的方向。 我正在用我所做的两次尝试发布整个class级。 public class Provider where T : class { protected readonly Repository Repository = null; private readonly string TEntityName = typeof(T).Name; [Inject] public Provider( Repository Repository) { this.Repository = Repository; } public virtual void Add( T TEntity) […]

如何将编译器检查的属性名称/表达式树传递给自定义属性

在一些地方,我注意到表达式树作为参数传递给方法,以允许编译器检查属性名称。 例如,Caliburn Micro在其PropertyChangedBase类中具有以下方法签名: public virtual void NotifyOfPropertyChange(Expression<Func> property); 我有一个自定义属性,我想在构造函数中使用相同类型的编译器检查属性名称,以使我能够键入: [MyCustomAttribute(() => PropertyName)] 代替: [MyCustomAttribute(“PropertyName”)] 使用构造函数定义: public MyCustomAttribute(params Expression<Func>[] properties) 但是,由于属性参数的限制是常量表达式,这似乎是不可能的。 任何人都可以推荐一种不同的方法,我可以让编译器检查我的属性参数中的属性名称,而不是留下这个只使用字符串的潜在错误? 编辑:感谢Marc的回答,我现在已经实现了这个: #if DEBUG foreach (var propertyInfo in GetType().GetProperties().Where(propertyInfo => Attribute.IsDefined(propertyInfo, typeof (MyCustomAttribute)))) { foreach (var propertyName in propertyInfo.GetAttributes(true) .SelectMany(attribute => attribute.PropertyNames)) Debug.Assert( GetType().GetProperty(propertyName) != null, “Property not found”, “Property {0} declared on attributed property […]

如何将CIL转换为LINQ表达式树

有没有关于将CIL直接转换为LINQ表达式树的工作? 这将包括类库,博客,书籍,学术论文等。 CIL和LINQ Expression API之间已知的不匹配是什么? 我见过一个项目 ,它显着扩展了Expression API,以支持C#和LINQ表达式之间几乎完整的映射。 建议采取哪些方法来解决这些不匹配问题? 直接从CIL转换为LINQ表达式有什么根本性的错误吗? 如果是这样,为什么?

如何创建表达式树以执行与“StartsWith”相同的操作

目前,我有这种方法来比较两个数字 Private Function ETForGreaterThan(ByVal query As IQueryable(Of T), ByVal propertyValue As Object, ByVal propertyInfo As PropertyInfo) As IQueryable(Of T) Dim e As ParameterExpression = Expression.Parameter(GetType(T), “e”) Dim m As MemberExpression = Expression.MakeMemberAccess(e, propertyInfo) Dim c As ConstantExpression = Expression.Constant(propertyValue, propertyValue.GetType()) Dim b As BinaryExpression = Expression.GreaterThan(m, c) Dim lambda As Expression(Of Func(Of T, Boolean)) = […]