expression-trees

“The binary operator Add is not defined for the types 'System.String' and 'System.String'.” — Really?

假装没事ソ 提交于 2019-12-10 12:33:24
问题 When trying to run the following code: Expression<Func<string, string>> stringExpression = Expression.Lambda<Func<string, string>>( Expression.Add( stringParam, Expression.Constant("A") ), new List<ParameterExpression>() { stringParam } ); string AB = stringExpression.Compile()("B"); I get the error referenced in the title: "The binary operator Add is not defined for the types 'System.String' and 'System.String'." Is that really the case? Obviously in C# it works. Is doing string s = "A" + "B

dynamic query using expression tree

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:03:28
问题 I have a form in which the user will choose the following from dropdown lists: table_name columnName_to_sort_by columnName_to_search_in The user shall enter Search_text in a text box The form shall draw data from many tables. I want to avoid writing the sort and search for every field for each of the tables. This is why I want to use expression trees. I want to build the query dynamically. I want to write a generic method that will generate the expression tree for the select , where and

a simple way to generate SQL Server's “standard” form of an expression?

こ雲淡風輕ζ 提交于 2019-12-10 09:41:12
问题 This relates to computed columns and default constraints (and possibly other expressions) in SQL Server 2005 (or above). Both of these use an arbitrary expression to generate a value, e.g. (year+1) for a computed column representing "next year" (this is obviously a simple and stupid example). What I'm trying to do: I want to be able to determine whether an existing computed column (or default constraint) in some table matches the intended definition, where the latter is defined in a software

how do I combine Expression<Func<MyClass,bool>>[]?

对着背影说爱祢 提交于 2019-12-10 05:15:11
问题 I have an array of Expression<Func<MyClass,bool>> However, I want to AND them all together to get just a single item of that type. How do I do this? Can I cast the result of Expression.And? 回答1: If you use the following extension method: public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ()); return Expression.Lambda<Func<T, bool>> (Expression

WhereNot linq expression

萝らか妹 提交于 2019-12-09 19:06:29
问题 I am trying to create an extension "WhereNot" So I can use: Dim x = "Hello world " Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty) Note that my aim here is to learn linq expressions; not solve my issue. I craated this function: <Extension()> _ Public Function WhereNot(Of TElement)(ByVal source As IQueryable(Of TElement), ByVal selector As Expression(Of Func(Of TElement, Boolean))) As IQueryable(Of TElement) Return source.Where(GetWhereNotExpression(selector)) End Function I don't

Building Expression Trees

﹥>﹥吖頭↗ 提交于 2019-12-09 18:16:45
问题 I'm struggling with the idea of how to build an expression tree for more lambdas such as the one below, let alone something that might have multiple statements. For example: Func<double?, byte[]> GetBytes = x => x.HasValue ? BitConverter.GetBytes(x.Value) : new byte[1] { 0xFF }; I would appreciate any thoughts. 回答1: I would suggest reading through the list of methods on the Expression class, all of your options are listed there, and the Expression Trees Programming Guide. As for this

Contravariance in Expressions

一曲冷凌霜 提交于 2019-12-09 16:06:54
问题 I'm trying to create a Generic Action Delegate delegate void ActionPredicate<in T1, in T2>(T1 t1, T2 t2); and public static ActionPredicate<T,string> GetSetterAction<T>(string fieldName) { ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target"); MemberExpression fieldExpr = Expression.Property(targetExpr, fieldName); ParameterExpression valueExpr = Expression.Parameter(typeof(string), "value"); MethodCallExpression convertExpr = Expression.Call(typeof(Convert), "ChangeType"

Building a MicroRuleEngine using LinqExpressions

落花浮王杯 提交于 2019-12-09 13:54:34
问题 So I am building a MicroRuleEngine (Would love to see this take off as an OpenSource project) and I am running into a null reference Error When executing the compiled ExpressionTree and I am not exactly sure why. Rules against the simple properties work but going against Child Properties aka Customer.Client.Address.StreetName etc. do not work. Below is the MicroRuleEngine using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions;

Best way to concat strings and numbers in SQL server using Entity Framework 5?

我是研究僧i 提交于 2019-12-09 13:18:54
问题 For some reason Microsoft decided to not support simple concat in EF5. e.g. Select(foo => new { someProp = "hello" + foo.id + "/" + foo.bar } This will throw if foo.id or foo.bar are numbers. The workaround I've found is apparently this pretty peice of code: Select(foo => new { someProp = "hello" + SqlFunctions.StringConvert((double?)foo.id).Trim() + "/" + SqlFunctions.StringConvert((double?)foo.bar).Trim() } Which works fine, but is just horrid to look at. So, is there some decent way to

Modify the expression tree of IQueryable.Include() to add condition to the join

六眼飞鱼酱① 提交于 2019-12-09 07:16:13
问题 Basically, I would like to implement a repository that filters all the soft deleted records even through navigation properties. So I have a base entity, something like that: public abstract class Entity { public int Id { get; set; } public bool IsDeleted { get; set; } ... } And a repository: public class BaseStore<TEntity> : IStore<TEntity> where TEntity : Entity { protected readonly ApplicationDbContext db; public IQueryable<TEntity> GetAll() { return db.Set<TEntity>().Where(e => !e