标签: expression-trees

否定表达式树中的方法调用

我正在动态生成ac#Linq表达式,如下所示(将在下面的示例中)运行string.Contains对集合值.

var dynamicMethod = "Contains";
var parameter = Expression.Parameter(typeof (MyClass), "type");
var property = Expression.Property(parameter, "MyProperty");
var constantValue = Expression.Constant("PropertyValue", property.Type);
var method = property.Type.GetMethod(dynamicMethod, new[] {property.Type});
var expression = Expression.Call(property, method, constantValue);
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我想要的东西相当于!包含.

有什么建议?

谢谢.

c# linq expression-trees

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

手动为x => x.Child == itemToCompare.Child构建linq表达式

我们有一个对象,我们想要动态地基于该对象构建一个linq查询.这个linq语句等同于我们想要构建的内容:

Expression<Func<Sample, bool>> linqExpression 
            = x => x.Child == itemToCompare.Child;
Run Code Online (Sandbox Code Playgroud)

我们无法提出构建itemToCompare.Child部分的正确表达式.这是我们到目前为止所拥有的:

var param = Expression.Parameter(typeof(T), "x");
var key = itemToCompare.GetType().GetProperty("Child");
var rhsConstant = Expression.Constant(item);
var innerLambda = Expression.Lambda<Func<T>>(rhsConstant, 
            new ParameterExpression[0]);
var rhsMemberAccess = Expression.MakeMemberAccess(innerLambda, key);
body = Expression.Equal(lhsPropertyAccess, rhsMemberAccess);
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
Run Code Online (Sandbox Code Playgroud)

我们手工构建的查询的表达式树如下所示:

Lambda Expression: x => (x.Child = value(SampleTests+Sample))
Expression Body: (x.Child = value(SampleTests+Sample))
Parameter 0: 'x', Type: Sample
NodeType: Lambda
   Left Lambda Expression: x.Child
   Left NodeType: MemberAccess
      Lambda Expression: x
      Expression Member: Sample Child
      NodeType: …
Run Code Online (Sandbox Code Playgroud)

linq lambda expression-trees

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

考虑以下BNF语法

考虑以下BNF语法(其中非终端包含在尖括号中并<identifier>与任何合法的Java变量标识符匹配).

<exp> ::= <exp> + <term>
      |   <exp> - <term>
      |   <term>
<term> ::= <term> * <factor>
       |   <term> / <factor>
       |   <factor>
<factor> ::= ( <exp> )
         |   <identifier>
Run Code Online (Sandbox Code Playgroud)

为以下表达式生成三个派生:

(x - a) * (y + b)
Run Code Online (Sandbox Code Playgroud)

盯着exp:

<exp>
Run Code Online (Sandbox Code Playgroud)

用术语替换exp:

<term>
Run Code Online (Sandbox Code Playgroud)

替换术语:

<term> * <factor>
Run Code Online (Sandbox Code Playgroud)

用因子代替术语:

<factor> * <factor>
Run Code Online (Sandbox Code Playgroud)

用(exp)替换这两个因子:

( <exp> ) * ( <exp> )
Run Code Online (Sandbox Code Playgroud)

用exp - term替换第一个exp,用exp + term替换第二个exp

( <exp> - <term> ) * ( <exp> + <term> )
Run Code Online (Sandbox Code Playgroud)

将exp替换为term,然后用因子替换所有4个术语.

( <factor> - …
Run Code Online (Sandbox Code Playgroud)

grammar bnf expression-trees

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

在Expression中使用string.Compare(a,b)

从昨天起我一直在教自己表达树,我在比较两个字符串值时遇到了问题.我已经使这个测试用例失败并出现错误:

No method 'Compare' on type 'System.String' is compatible with the supplied arguments.

在运行时失败了 left = Expression.Call(

Type type = typeof(string);
Expression left, right;
left = Expression.Constant("1", type);
right = Expression.Constant("2", type);
// fails at run-time on the next statement
left = Expression.Call(
    typeof(string),
    "Compare",
    new Type[] { type, type },
    new Expression[] { left, right });
right = Expression.Constant(0, typeof(int));
Run Code Online (Sandbox Code Playgroud)

我将在a中使用结果左右Expression.Equal, LessThan, LessThanOrEqual, GreaterThan or GreaterThanOrEqual.这就是Compare方法的原因.

我确信它很简单,我把我的代码简化为这个简单的测试用例.谁知道我哪里出错了?

c# expression expression-trees

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

如何在c#中通过表达式构建动态查询

首先,我有一个如下变量:

List<string> values;
Run Code Online (Sandbox Code Playgroud)

现在我需要构建查询条件,如下所示:

Expression<Func<docinstance, bool>> filter = d=>d.values.any(o=>o.value==values[0]||o.value==value[1]||.....)
Run Code Online (Sandbox Code Playgroud)

因为我不知道变量值中有多少项,所以如何构建查询条件

c# entity-framework expression-trees

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

表达式解析 - 可以将属性名称数组作为字符串?

有可能完成这个方法吗?是否可以在最新版本的C#中使用?将此视为DSL,以配置系统以查看某些对象上的某些属性更改.

List<string> list = GetProps<AccountOwner>(x => new object[] {x.AccountOwnerName, x.AccountOwnerNumber}); 
// would return "AccountOwnerName" and "AccountOwnerNumber"

public List<string> GetProps<T>(Expression<Func<T, object[]>> exp)
{  
    // code here
}
Run Code Online (Sandbox Code Playgroud)

.net c# dsl expression expression-trees

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

使用此代码和表达式api无法获取此属性名称

我有以下类,我需要获取其属性名称:

public class PMLButtonData
{
    public int BackgroundColorID
    {
        get;
        set;
    }

    public string Callback
    {
        get;
        set;
    }
}
Run Code Online (Sandbox Code Playgroud)

要获取我正在使用此功能的名称

public static string GetPropertyName<T>(Expression<Func<T, object>> lambda)
{
    MemberExpression member = lambda.Body as MemberExpression;
    PropertyInfo property = member.Member as PropertyInfo;

    return property.Name;
}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下代码获取Callback属性名称:

string name = GetPropertyName<PMLButtonData>(x => x.Callback);
Run Code Online (Sandbox Code Playgroud)

但是其他属性的相同代码不起作用:

string name = GetPropertyName<PMLButtonData>(x => x.BackgroundColorID);
Run Code Online (Sandbox Code Playgroud)

它们之间的唯一区别是数据类型,因此我将Callback更改为int,并且代码不再适用于此属性.如果它是一个整数,为什么我不能这样得到属性的名称?

c# reflection expression-trees

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

Expression.Call GroupBy然后选择和Count()?

使用表达式树,我需要以通用方式构建GroupBy.我要使用的静态方法如下:

public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String coloumn)
{

  //Code here

}
Run Code Online (Sandbox Code Playgroud)

Result类有两个属性:

public string Value { get; set; }
public int Count { get; set; }
Run Code Online (Sandbox Code Playgroud)

基本上我想通过表达式树构建以下Linq查询:

query.GroupBy(s => s.Country).Select(p => new 
                {
                    Value = p.Key,
                    Count = p.Count()
                }
            )
Run Code Online (Sandbox Code Playgroud)

你会如何实现它?

c# linq expression-trees

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

如何插入Julia"for"表达式?

我正在编写一个基于Python的列表推导的宏@vcomp(矢量理解)和一个条件子句,以简洁的方式过滤元素.

macro vcomp(comprehension::Expr, when::Symbol, condition)
    comp_head, comp_args = comprehension.head, comprehension.args
    comp_head ? [:comprehension, :typed_comprehension] && error("@vcomp not a comprehension")
    when ? :when && error("@vcomp expected `when`, got: `$when`")
    T = comp_head == :typed_comprehension ? comp_args[1] : nothing
    if VERSION < v"0.5-"
        element  = comp_head == :comprehension ? comp_args[1] : comp_args[2]
        sequence = comp_head == :comprehension ? comp_args[2] : comp_args[3]
    else
        element  = comp_head == :comprehension ? comp_args[1].args[1] : comp_args[2].args[1]
        sequence = comp_head == :comprehension ? comp_args[1].args[2] …
Run Code Online (Sandbox Code Playgroud)

metaprogramming abstract-syntax-tree expression-trees julia

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

Linq查询 - List <T>上的"Where"其中reflect属性包含text/value

我想构建一个函数,用户可以搜索列表中的某些属性是否包含值

假设我们将拥有List,并且Company将被定义为具有以下属性的类:

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string CompanyAddress1 { get; set; }
    public string CompanyPostCode { get; set; }
    public string CompanyCity { get; set; }
    public string CompanyCounty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在 - 理想情况下我希望有这个参数的功能

List<Company> FilterCompanies(List<Company> unfilteredList, string fieldToQueryOn, string query)
{
    // linq  version what ideally would like to archeve
    return unfilteredList.Where(x => x."fieldToQueryOn".ToString().ToLower().Contains(query.ToLower())).ToList();
}
Run Code Online (Sandbox Code Playgroud)

并致电:

var variable = FilterCompanies(NotNullFilledUnfilteredList, "CompanyCity", …
Run Code Online (Sandbox Code Playgroud)

c# linq expression-trees

1
推荐指数
2
解决办法
811
查看次数