标签: expression-trees

将Linq转换为Sql表达式到表达式树

任何人都可以将这个简单的LINQ-to-SQL转换为表达式树:

List<Region> lst = (from r in dc.Regions
                    where r.RegionID > 2 && r.RegionDescription.Contains("ern")
                    select r).ToList();
Run Code Online (Sandbox Code Playgroud)

expression expression-trees linq-to-sql c#-4.0

0
推荐指数
1
解决办法
4295
查看次数

深度> 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; }
}
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个成员访问Employee.Name的表达式树,这就是我所做的:

        var param = Expression.Parameter(type, "x");
        var memberAccess = Expression.PropertyOrField(param, memberName);
        return Expression.Lambda<Func<TModel, TMember>>(memberAccess, param);
Run Code Online (Sandbox Code Playgroud)

成员访问Employee.Job.Salary的等价物是什么?

c# linq lambda expression expression-trees

0
推荐指数
1
解决办法
1915
查看次数

从后缀表示法构建表达式树的问题

我正在为简单的数学表达式(常量和简单算术)编写一个推动者.

我遇到的问题是从后缀格式化表达式构建表达式树.我在大多数情况下所做的工作都很好,但维基百科没有这个例子.

如果我评估表达式3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3,3,0001220703125即使结果应该是,我得到结果3,001953125.这样做的原因似乎是表达式树看起来像3+((4*2)/((1-5)^(2^3)))而不是(3+((4*2)/(((1-5)^2)^3))).

原始表达式的后缀表示法看起来像 3 4 2 * 1 5 ? 2 3 ^ ^ / +

有关如何获取表达式树的任何建议,我希望它是什么?
下面是表达式树代码的后缀和一些在C#中的测试,但应该是非常明显的.

public MathExpression Parse()
{
    var tokens = this.ToPostFix(_tokens);
    var stack = new Stack<MathExpression>();
    foreach(token in tokens)
    {
        if(token.IsOperand())
        {
            // Push the operand on the stack.
            stack.Push(new ConstantExpression(token.Value));
        }
        else
        {
            Debug.Assert(token.Type == TokenType.Operator, "Expected …
Run Code Online (Sandbox Code Playgroud)

language-agnostic algorithm expression-trees postfix-notation

0
推荐指数
1
解决办法
1528
查看次数

如何为多列 OrderBy 表达式创建表达式树

我已经为 EF 通用存储库创建了一个 orderby 表达式,如下 string command = orderByDesc ? "排序依据降序" : "排序依据";

var type = typeof(T);

var property = type.GetProperty(orderby);

var parameter = Expression.Parameter(type, "p");

var propertyAccess = Expression.MakeMemberAccess(parameter, property);

var orderByExpression = Expression.Lambda(propertyAccess, parameter);

var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },

                       items.Expression, Expression.Quote(orderByExpression));
items = items.Provider.CreateQuery<T>(resultExpression);
Run Code Online (Sandbox Code Playgroud)

现在我想创建包含 2 列的表达式用于排序,但无法找到有用的内容。

请帮我创建一个包含 2 列的 orderby 表达式。

.net lambda expression expression-trees

0
推荐指数
1
解决办法
1975
查看次数

我不明白这个表达式树

public static Expression<Func<int, int, int>> CreateExpressionTreeLambdaExpression()
{
    return (x, y) => x * y;
}
Run Code Online (Sandbox Code Playgroud)

所以我理解,如果我想测试这个方法,我可以这样做,评估为真.

[TestMethod]
public void TestLambdaExpressions_ExpressionTreeLambdaExpression()
{
    var expression = MultiplyDelegate.CreateExpressionTreeLambdaExpression();
    var function = expression.Compile();
    Assert.AreEqual(6, function(2, 3));
}
Run Code Online (Sandbox Code Playgroud)

我似乎没有掌握的是Expression中的三个int.第三个int在哪里使用?这是对自己的某种引用吗?

c# expression-trees

0
推荐指数
1
解决办法
66
查看次数

如何在表达式树中编写新的List <CustomClass>()?

我想知道如何在表达式树中代表c#代码.

var list = new List<CustomClass>(); 
list.add(new CustomClass());
Run Code Online (Sandbox Code Playgroud)

其中CustomClass是一些复杂的类型.

就像使用Expression.NewArrayInit初始化数组一样

谢谢

c# expression-trees

0
推荐指数
1
解决办法
146
查看次数

如何将viewmodel修改为Expression <Func <T,bool >>?

捎带一个非常相似的问题 ......

我需要从ViewModel生成一个表达式作为搜索谓词传递.我需要能够根据用户提供的内容包含/排除查询参数.例:IQueryable.Where

public class StoresFilter
{
    public int[] Ids { get; set; }

    [StringLength(150)]
    public string Name { get; set; }

    [StringLength(5)]
    public string Abbreviation { get; set; }

    [Display(Name = "Show all")]
    public bool ShowAll { get; set; } = true;

    public Expression<Func<Store, bool>> ToExpression()
    {
        List<Expression<Func<Store, bool>>> expressions = new List<Expression<Func<Store, bool>>>();

        if (Ids != null && Ids.Length > 0)
        {
            expressions.Add(x => Ids.Contains(x.Id));
        }
        if (Name.HasValue())
        {
            expressions.Add(x => x.Name.Contains(Name)); …
Run Code Online (Sandbox Code Playgroud)

c# ienumerable iqueryable func expression-trees

0
推荐指数
1
解决办法
120
查看次数

编译表达式比反射慢

我有一个具有动态集的PropertyInfo.SetValue.意味着要设置的值是未知的.

我从互联网上得到了这样的方法.

private static Action<object, object> CreateSetAccess(MethodInfo method)
{
    var obj = Expression.Parameter(typeof(object), "o");
    var value = Expression.Parameter(typeof(object));

    Expression<Action<object, object>> expr =
        Expression.Lambda<Action<object, object>>(
            Expression.Call(
                Expression.Convert(obj, method.DeclaringType),
                method,
                Expression.Convert(value, method.GetParameters()[0].ParameterType)),
            obj,
            value);

    return expr.Compile();
}
Run Code Online (Sandbox Code Playgroud)

这样做是创建一个表达式并对其进行编译,但是使用参数类型转换对象.

我像这样消费它.

var method2 = CreateSetAccess(property.GetSetMethod());
method2(response, valueToSet);
Run Code Online (Sandbox Code Playgroud)

发生的事情似乎是慢了 PropertyInfo.SetValue

这是我的基准

var xpathNavigator = XmlHelper.CreateXPathDocument(serviceResponse).CreateNavigator();
foreach (var propertyInformation in propertyInformationSource)
{
    // Gets the node using the NodePath provided in the Attribute
    var attr = propertyInformation.Value;
    var pathValue = xpathNavigator.SelectSingleNode(attr.NodePath);
    if (pathValue == null)
        continue; …
Run Code Online (Sandbox Code Playgroud)

c# performance expression-trees

-1
推荐指数
1
解决办法
644
查看次数