相关疑难解决方法(0)

在不使用GetMethods的情况下获取泛型方法

我想得到方法System.Linq.Queryable.OrderyBy<T, TKey>(the IQueryable<T> source, Expression<Func<T,TKey>> keySelector)方法,但我不断提出空值.

var type = typeof(T);
var propertyInfo = type.GetProperty(group.PropertyName);
var propertyType = propertyInfo.PropertyType;

var sorterType = typeof(Func<,>).MakeGenericType(type, propertyType);
var expressionType = typeof(Expression<>).MakeGenericType(sorterType);

var queryType = typeof(IQueryable<T>);

var orderBy = typeof(System.Linq.Queryable).GetMethod("OrderBy", new[] { queryType, expressionType }); /// is always null.
Run Code Online (Sandbox Code Playgroud)

有没有人有任何见解?我宁愿不循环GetMethods结果.

.net c# generics reflection

26
推荐指数
5
解决办法
1万
查看次数

使用MongoDB C#驱动程序从父子层次结构中查找并更新节点

我有一个分层的类别文档,如父母 - 儿童 - 儿童等......

{
    id: 1,
    value: {

    }Children: [{
        id: 2,
        value: {

        }Children: [{
            id: 3,
            value: {

            }Children: [{
                id: 4,
                value: {

                }Children: [{
                    id: 5,
                    value: {

                    }Children: [{
                        id: 6,
                        value: {

                        }Children: [{
                            id: 7,
                            value: {

                            }Children: []
                        }]
                    }]
                }]
            }]
        }]
    }]
}
Run Code Online (Sandbox Code Playgroud)

在这样的文档中,使用MongoDB C#驱动程序,我该如何找到一个节点 Id = x

我试过这样的事

var filter = Builders<Type>.Filter.Eq(x => x.Id, 3);
                var node = mongoDbRepository.GetOne(filter) ??
                             mongoDbRepository.GetOne(Builders<Type>.Filter.Where(x => x.Children.Any(c=>c.Id == 3)));
Run Code Online (Sandbox Code Playgroud)

但这仅涵盖两个层面.在我的例子中,我有7个级别,我没有对级别深度的限制

一旦我找到该节点,我需要 …

c# mongodb mongodb-query mongodb-.net-driver

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

从表达式调用方法

使用Expression.Call时,方法"Any"有哪些类型和参数?

我有一个内部和外部表达式,我想与Any一起使用.表达式以编程方式构建.

内在(这是有效的):

ParameterExpression tankParameter = Expression.Parameter(typeof(Tank), "t");
Expression tankExpression = Expression.Equal(
    Expression.Property(tankParameter, "Gun"),
    Expression.Constant("Really Big"));

Expression<Func<Tank, bool>> tankFunction = 
    Expression.Lambda<Func<Tank, bool>>(tankExpression, tankParameter); 
Run Code Online (Sandbox Code Playgroud)

外面(看起来正确):

ParameterExpression vehicleParameter = Expression.Parameter(typeof(Vehicle), "v");

Expression vehicleExpression = Expression.Lambda(
    Expression.Property(
        vehicleParameter, 
        typeof(Vehicle).GetProperty("Tank")), 
    vehicleParameter);
Run Code Online (Sandbox Code Playgroud)

这给了我两个表达式:

v => v.Tank
t => t.Gun == "Really Big";
Run Code Online (Sandbox Code Playgroud)

而我正在寻找的是:

v => v.Tank.Any(t => t.Gun == "Really Big");
Run Code Online (Sandbox Code Playgroud)

我试图使用Expression.Call方法来使用"Any".这是正确的方法吗?2.以下引发异常,"类型'System.Linq.Queryable'上没有方法'Any'与提供的参数兼容."

这是我如何调用任何:

Expression any = Expression.Call(
    typeof(Queryable),
    "Any",
    new Type[] { tankFunction.Body.Type }, // this should match the delegate...
    tankFunction);
Run Code Online (Sandbox Code Playgroud)

如何将来自vehicleExpression的Any链接到tankFunction?

c# lambda

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

如何使用带有lambda参数的typeof(object).GetMethod()

如何使用Type.GetMethod()来获取具有lambda参数的方法?我正在尝试使用以下方法获取类似Func的参数的Queryable.Any方法:

typeof(Queryable).GetMethod("Any", new Type[]{typeof(Func<ObjType, bool>)})
Run Code Online (Sandbox Code Playgroud)

但它一直返回null.

c# lambda

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