Tag: linq expressions

从linq表达式中检索信息时是否使用了reflection?

我有以下扩展方法: public static string ToPropertyName(this Expression<Func> propertyExpression) { if (propertyExpression == null) return null; string propName; MemberExpression propRef = (propertyExpression.Body as MemberExpression); UnaryExpression propVal = null; // — handle ref types if (propRef != null) propName = propRef.Member.Name; else { // — handle value types propVal = propertyExpression.Body as UnaryExpression; if (propVal == null) throw […]

结合多个Linq表达式

我正在重构一些代码,试图让它更自我记录。 当前代码对OData服务进行查询,如下所示: return context.MessageLog.Where ( x => ( x.Status == MessageStatus.Success || x.Status == MessageStatus.Failure ) && x.Direction == MessageDirection.Inbound && x.ResponseDate == new DateTimeOffset(new DateTime(1900, 01, 01)) ); 我希望改变它以使用Linq Expressions。 我可以将所有逻辑移动到单个表达式中,并使代码运行context.MessageLog.Where(MessageIsPendingResponse); 。 但是,我想为不同的条件创建表达式: MessageIsProcessed (即现在处于成功或失败状态), MessageIsInbound和ResponseNotYetSent (响应日期为null)。 我可以将这些与多个where语句结合起来,如下所示: return context.MessageLog .Where(MessageLogExpression.MessageIsProcessed) .Where(MessageLogExpression.MessageIsInbound) .Where(MessageLogExpression.ResponseNotYetSent); ( MessageLogExpression是我用来包含这些预定义表达式的类)。 问题1 这是组合声明的最佳方式,还是首先冒险过滤错误的字段(例如,Linq是否将所有条件合并到一个查询中并允许查询引擎(以SQL术语)确定最佳执行计划;或者我们是否强制它首先在状态字段中过滤? 问题2 以上内容适用于我们有AND加入表达式的场景; 但我们怎么做OR呢? 我假设有一些方法来组合这些,但找不到任何明显的东西。 我怀疑这样的事情存在吗? return context.MessageLog.Where(new […]

通用表达式抽象问题

我有以下方法SetMapping() ,用于使用表达式定义一些映射设置。 public class AggregateMap { protected Expression<Func<IUpdateConfiguration, object>> graphMapping; protected void SetMapping(Expression<Func<IUpdateConfiguration, object>> mapping) { graphMapping = mapping; } } 调用代码示例: SetMapping(map => map.OwnedCollection(root => root.ChildEntities)); 上面的工作很好,但我想通过提供SetOwnedCollectionMapping()进一步抽象这个方法。 这意味着调用代码可以提供更基本的表达式。 进一步抽象的方法: protected void SetOwnedCollectionMapping(Expression<Func<TDataEntity, ICollection>> mapping) { graphMapping = map => map.OwnedCollection(mapping); } 调用代码示例: SetOwnedCollectionMapping(root => root.ChildEntities); 然后,通过在Entity Framework DbContext实例上调用以下方法,将此graphMapping字段用于外部库(RefactorThis.GraphDiff): public static void UpdateGraph(this DbContext context, […]

使用LINQkit在LINQ to Entities Select中调用Expression

这就是我想要做的: class MyDbContext : DbContext { private static Expression<Func> myExpression1 = x => /* something complicated … */; private static Expression<Func> myExpression2 = x => /* something else complicated … */; public object GetAllData() { return ( from o in MyClassDbSet.AsExpandable() select new { data1 = myExpression1.Invoke(o), // problem 1 data2 = o.Items.Select(myExpression2.Compile()) // problem […]

包含的LINQ表达式

我想在linq中添加动态表达式,但面对包含方法的问题,它对Equal方法非常有效 问题是我正在动态地获取FilterField如何在查询中替换 到目前为止,我曾尝试过 List Ids = new List(); **string filterField =”DEPARTMENT”; ==> Dynamic Field** var eParam = Expression.Parameter(typeof(EmployeeDetail), “e”); var comparison = Expression.Equal(Expression.Property(eParam, filterField), Expression.Convert(Expression.Constant(Ids), Expression.Property(eParam, filterField).Type)); var lambda = Expression.Lambda<Func>(comparison, eParam); var countMonthly1 = ctx.tblMonthlyInput.Join(ctx.tblEmployee, a => a.CompanyId, b => b.CompanyId, (a, b) => b).Where(lambda).Count(); 我想使用linq表达式为Contains方法进行上述查询工作 样本查询: var countMonthly = (from a in ctx.tblMonthlyInput […]

Dynamic Func <IQueryable ,IOrderedQueryable >表达式

我正在使用这里提到的模式http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in- AN-ASP净MVC应用程序 我正在使用下面的方法来查询EF public virtual IEnumerable Get( Expression<Func> filter = null, Func<IQueryable, IOrderedQueryable> orderBy = null, string includeProperties = “”) { IQueryable query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ‘,’ }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { […]

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

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

如何在没有编译的情况下从现有的LambdaExpression构建LambdaExpression

我想结合两个LambdaExpressions而不编译它们。 这就是我编译它们时的样子: public Expression<Func> CreatePredicate( Expression<Func> getMemberExpression, Expression<Func> memberPredicateExpression) { return x => memberPredicateExpression.Compile()(getMemberExpression.Compile()(x)); } 这显然不是从提供的参数中获取目标表达式的最快方法。 此外,它使它与不支持C#方法调用的LINQ to SQL等查询提供程序不兼容。 从我所看到的,似乎最好的方法是构建一个ExpressionVisitor类。 然而,这似乎是一个非常常见的任务。 有没有人知道提供这种function的现有开源代码库? 如果没有,那么接近ExpressionVisitor以使其尽可能通用的最佳方法是什么?

如何将LambdaExpression转换为类型化表达式<Func >

我正在为nHibernate动态构建linq查询。 由于依赖关系,我想在以后转换/检索键入的表达式,但到目前为止我还没有成功。 这不起作用(演员应该发生在其他地方): var funcType = typeof (Func).MakeGenericType(entityType, typeof (bool)); var typedExpression = (Func)Expression.Lambda(funcType, itemPredicate, parameter); //Fails 这是有效的: var typedExpression = Expression.Lambda<Func>(itemPredicate, parameter); 是否有可能从LambdaExpression获取’封装’类型表达式?

将lambda表达式转换为用于缓存的唯一键

我已经看过类似这个问题的其他问题,但我找不到任何可行的答案。 我一直在使用以下代码生成唯一键,用于将我的linq查询的结果存储到缓存中。 string key = ((LambdaExpression)expression).Body.ToString(); foreach (ParameterExpression param in expression.Parameters) { string name = param.Name; string typeName = param.Type.Name; key = key.Replace(name + “.”, typeName + “.”); } return key; 它似乎适用于包含整数或布尔值的简单查询,但是当我的查询包含嵌套的常量表达式时,例如 // Get all the crops on a farm where the slug matches the given slug. (x => x.Crops.Any(y => slug == y.Slug) && […]