相关疑难解决方法(0)

如何创建一个调用IEnumerable <TSource> .Any(...)的表达式树?

我正在尝试创建一个表示以下内容的表达式树:

myObject.childObjectCollection.Any(i => i.Name == "name");
Run Code Online (Sandbox Code Playgroud)

为清楚起见,我有以下内容:

//'myObject.childObjectCollection' is represented here by 'propertyExp'
//'i => i.Name == "name"' is represented here by 'predicateExp'
//but I am struggling with the Any() method reference - if I make the parent method
//non-generic Expression.Call() fails but, as per below, if i use <T> the 
//MethodInfo object is always null - I can't get a reference to it

private static MethodCallExpression GetAnyExpression<T>(MemberExpression propertyExp, Expression predicateExp)
{
    MethodInfo method = typeof(Enumerable).GetMethod("Any", new[]{ typeof(Func<IEnumerable<T>, Boolean>)});
    return …
Run Code Online (Sandbox Code Playgroud)

.net c# linq expression-trees

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

为实体框架构建嵌套的lambda表达式树多对多关系?

我正在尝试构建一个实体框架4多对多关系过滤器,这是一个动态数据项目.我知道我需要在运行时构建一个表达式树,而且我很熟悉这样做类似于这样的表达式:

private MethodCallExpression BuiltMethodCall(IQueryable _query, Type _ObjType, string _ColumnToSearch, string _SearchValue)
{
ConstantExpression value = Expression.Constant(_SearchValue);
ParameterExpression _parameter = Expression.Parameter(_ObjType, "value");
MemberExpression _property = Expression.Property(_parameter, _ColumnToSearch);
BinaryExpression comparison = Expression.Equal(_property, value);
LambdaExpression lambda = Expression.Lambda(comparison, _parameter);

//Ex: Customers.Select(c => c).Where(value => (value.City == "Seattle"))
MethodCallExpression _where = Expression.Call(typeof(Queryable), "Where", new Type[] { _query.ElementType }, new Expression[] {
    _query.Expression,
    Expression.Quote(lambda)
});
return _where;

}
Run Code Online (Sandbox Code Playgroud)

为简单起见,这些示例使用Northwind数据库,其中有多个(Customers < - CustomerCustomerDemo - > CustomerDemographics)的连接.我遇到的问题是在下面的表达式中有嵌套的lambda时构建表达式树,我在这里检索具有特定客户人口统计的所有客户.

string custDemogID = "3";

//Get customers who have a particular …
Run Code Online (Sandbox Code Playgroud)

.net linq many-to-many entity-framework expression-trees

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