相关疑难解决方法(0)

内部.NET Framework数据提供程序错误1025

IQueryable<Organization> query = context.Organizations;

Func<Reservation, bool> predicate = r => !r.IsDeleted;

query.Select(o => new { 
    Reservations = o.Reservations.Where(predicate)
}).ToList();
Run Code Online (Sandbox Code Playgroud)

此查询引发"内部.NET Framework数据提供程序错误1025"异常,但下面的查询不会.

query.Select(o => new { 
    Reservations = o.Reservations.Where( r => !r.IsDeleted)
}).ToList();
Run Code Online (Sandbox Code Playgroud)

我需要使用第一个,因为我需要检查一些if语句来构造正确的谓词.我知道在这种情况下我不能使用if语句,这就是我将委托作为参数传递的原因.

如何使第一个查询起作用?

.net c# entity-framework expression-trees entity-framework-4

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

LinqKit System.InvalidCastException在成员属性上调用方法提供的表达式时

给定一个简单的父/子类结构.我想使用linqkit在父级上应用子lambda表达式.我还希望Lambda表达式由实用程序方法提供.

public class Foo
{
    public Bar Bar { get; set; }
}

public class Bar
{
    public string Value { get; set; }
    public static Expression<Func<Bar, bool>> GetLambdaX()
    {
        return c => c.Value == "A";
    }
}
...

Expression<Func<Foo, bool>> lx = c => Bar.GetLambdaX().Invoke(c.Bar);
Console.WriteLine(lx.Expand());
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出

System.InvalidCastException: Unable to cast object of type 
'System.Linq.Expressions.MethodCallExpression' to type 
'System.Linq.Expressions.LambdaExpression'.
at LinqKit.ExpressionExpander.VisitMethodCall(MethodCallExpression m)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.ExpressionVisitor.VisitLambda(LambdaExpression lambda)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.Extensions.Expand<TDelegate>(Expression`1 expr)
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework expression-trees linqkit

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