Tag: 表达式树

将变量注入Mono.CSharp.Evaluator(运行时从字符串编译LINQ查询)

我正在使用Mono.CSharp库来发出代码。 关于SO的另一个问题(http://stackoverflow.com/questions/3407318/mono-compiler-as-a-service-mcs)我设法在Microsoft CLR上正确评估Mono.CSharp。 为了增加我的应用程序的灵活性,我希望能够在运行时自定义查询 – 允许用户提供LINQ查询作为一个字符串,在执行时会被解析并命中数据库。 鉴于这个基本的代码片段: IQueryable contacts = GetContacts(); string query = “from contact in contacts where contact.Name == \”name\” select contact”; var queryableResult = Mono.CSharp.Evaluator.Evaluate(query); 如何将contacts变量“注入”Mono.CSharp.Evaluator以作为查询的一部分进行评估? 我是以正确的方式来做这件事的吗? 最后,我需要结果表达式或’query’字符串中的IQueryable。

Lambda参数不在范围内 – 构建二进制lambda表达式时

手动创建lambda表达式时,我得到一个“参数不在范围内”的exception。 关于我做错了什么的任何想法? public class OtherType { public string First_Name { get; set; } public string Last_Name { get; set; } } static void Main(string[] args) { Expression<Func> l2 = p => p.First_Name == “Bob”; l2.Compile(); // Works PropertyInfo property = typeof(OtherType).GetProperty(“First_Name”); ParameterExpression para = Expression.Parameter(typeof(OtherType), “para”); ConstantExpression right = Expression.Constant(“Bob”, typeof(string)); MemberExpression left = Expression.Property(Expression.Parameter(typeof(OtherType), […]

NHibernate中的表达树

我有一个有这个签名的方法 public static IList GetBy(System.Linq.Expressions.Expression<Func> expression) 我用来传递lambda表达式并通过从expressiontree中检索数据来在nhibernate中进行搜索限制。 所以当类用户传递类似: c => c.fullName == “John” && c.lastName == “Smith” && c.lastName != “” || c.fullName != “” && c.Age > 18 我从表达式树中读取这个结构,这样我就有了一个完整的intellisense方法来提供搜索条件 换句话说:我需要将搜索条件传递给数据访问层(Nhibernate) 所以我需要从表达式树中提取标准,然后通过示例将其传递给n hibernate: c=>c.fullname = “John” 我将从表达式树中提取以下信息: propertyname = fullname , value = “John” , restrictiontype = “equality” 然后将此信息传递给nhibernate,如下所示: ICriteria crit = session.CreateCriteria(typeof(T)); crit.Add(Restrictions.Eq(propretyName, value)); […]

在字符串上构建表达式时如何实现LessThan等

我有一个包,我正在构建表达式树,通过PredicateBuilder与EntityFramework一起使用: public Expression<Func> constructaPredicate(ExpressionType operation, string fieldName, Expression value) { var type = typeof(T); var parameter = Expression.Parameter(type); var member = Expression.PropertyOrField(parameter, fieldName); Expression comparison = Expression.MakeBinary(operation, member, value); var expression = Expression.Lambda<Func>(comparison, parameter); return expression; } 这种方法很好,除了比较字符串和GreaterThan等。在这种情况下,我得到一个例外: The binary operator GreaterThan is not defined for the types ‘System.String’ and ‘System.String’. 这很简单。 浏览一下,我发现只有少数几个引用这个问题,没有在我正在做的事情的背景下。 当然,问题是没有String.GreaterThan方法。 通常的答案是使用String.CompareTo(),但我还没弄清楚如何使这项工作。 […]

带有“OR”子句的通用表达式树,用于每个提供的属性

我为IQueryable创建了一个通用的搜索扩展方法,使您可以搜索单个属性以查看其中是否包含搜索词。 http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable 我现在想让用户选择多个属性在每个属性中进行搜索,如果任何属性包含文本,则匹配。 代码: 用户输入以下代码以执行此搜索: string searchTerm = “Essex”; context.Clubs.Search(searchTerm, club => club.Name, club => club.County) //Note: If possible I would rather something closer to the following syntax… context.Clubs.Search(club => new[]{ club.Name, club.County}, searchTerm); // … or, even better, something similar to this… context.Clubs.Search(club => new { club.Name, club.County}, searchTerm); 这将返回任何名为“Essex”的高尔夫俱乐部或郡。 public static IQueryable Search(this […]

如何将LINQ查询的一部分移动到可重用的LINQ表达式树

我想通过使用表达式树来重复使用LINQ查询的一部分(我认为这就是它的调用方式)。 这是我的查询的简化版本: var lQuery = from m in … join a in … into ta from ra in ta.DefaultIfEmpty() select new { … Status = ra != null ? … : …, /* <- i want this to be reusable */ … }; 如您所见,Status的值是通过? : ? :语法由LINQ自动翻译(我想是一个表达式树,SQL日志最后显示一个CASE WHEN查询)。 如何将该代码移动到单独的函数,以便我的应用程序不会在运行时抛出not supportedexception? 我尝试添加一个function public static System.Linq.Expressions.Expression<System.Func> QueryStatusExpression() […]

为lambda声明提供的参数数量不正确

请看一下我的代码:我正在尝试用表达式树创建myCar 。 我在这行上得到一个ArgumentException var m = Expression.Lambda<Func<Engine,…消息是为lambda声明提供的参数数量不正确 。 public class Engine { public string Name { get; private set; } public Engine(string name) { Name = name; } } public class Car { private readonly Engine engine; public Car(Engine engine) { this.engine = engine; } public string GetEngineName(){return engine.Name;} } class Program { static void […]

C#:如何将任意字符串解析为表达式树?

在我正在研究的项目中,我必须使用相当奇怪的数据源。 我可以给它一个“查询”,它会返回一个DataTable。 但查询不是传统的字符串。 它更像是……一组定义我想要的标准的方法调用。 这些方面的东西: var tbl = MySource.GetObject(“TheTable”); tbl.AddFilterRow(new FilterRow(“Column1”, 123, FilterRow.Expression.Equals)); tbl.AddFilterRow(new FilterRow(“Column2”, 456, FilterRow.Expression.LessThan)); var result = tbl.GetDataTable(); 本质上,它支持所有标准的东西(布尔运算符,parantheses,一些函数等),但是编写它的语法对于日常使用来说非常冗长和不舒服。 我想创建一个解析给定表达式的小解析器(如”Column1 = 123 AND Column2 < 456" )并将其转换为上述函数调用。 此外,如果我可以在那里添加参数将是很好的,所以我将受到保护免受注入攻击。 顶部的最后一小块糖将是如果它可以缓存解析结果并在要在另一个对象上重新执行相同的查询时重用它们。 所以我想知道 – 我可以使用任何现有的解决方案,还是我必须推出自己的表达式解析器? 这不是太复杂,但如果我可以节省两三天的编码和一大堆错误来修复,那将是值得的。

如何获取IQueryable.Count的ToTraceString

我使用((ObjectQuery)IQueryable).ToTraceString()来获取和调整将由LINQ执行的SQL代码。 我的问题是,与大多数IQueryable方法不同,IQueryable.Count定义如下: public static int Count(this IQueryable source) { return (int)source.Provider.Execute( Expression.Call( typeof(Queryable), “Count”, new Type[] { source.ElementType }, source.Expression)); } 执行查询而不编译并返回IQueryable。 我想通过这样的方式来做这个伎俩: public static IQueryable CountCompile(this IQueryable source) { return source.Provider.CreateQuery( Expression.Call( typeof(Queryable), “Count”, new Type[] { source.ElementType }, source.Expression)); } 但是然后CreateQuery给了我以下exception: LINQ to Entities query expressions can only be constructed from instances that […]

MemberExpression:从范围”引用的InvalidOperationExpression变量’x’,但未定义

我正在使用System.Linq.Expressions 我试图构建一个包含MemberExpression的简单LambdaExpression 。 如果我使用System.Linq.Expressions API(例如MakeMemberAccess )显式创建MemberExpression,我将在我调用Compile()时得到错误“InvalidOperationExpression变量’x’从范围”引用,但它没有定义” LambdaExpression。 例如,这是我的代码 Expression<Func> expression1, expression2, expression3; Func fn; expression1 = (x) => x.Title; fn = expression1.Compile();//works fn(c); MemberExpression m; m = Expression.MakeMemberAccess( Expression.Parameter(typeof(Customer), “x”), typeof(Customer).GetProperty(“Title”)); expression2 = Expression.Lambda<Func>(m, Expression.Parameter(typeof(Customer), “x”)); m = Expression.Property(Expression.Parameter(typeof(Customer),”x”), “Title”); expression3 = Expression.Lambda<Func>(m, Expression.Parameter(typeof(Customer), “x”)); fn = expression3.Compile();//InvalidOperationExpression variable ‘x’ referenced from scope ”, but […]