其中子句使用导航属性例外

本文关键字:属性 导航 子句 | 更新日期: 2023-09-27 18:33:52

我首先使用 C#、.NET Framework 4.0 和实体框架代码开发 WCF RESTful Web 服务。

我有这个代码:

var users = from u in context.Users.Include("WantsToDo")
where u.WantsToDo.Contains(context.Activities.Find(activityId)) &&
    u.Gender == genderId &&
    u.City == city &&
    u.Country == countryIsoCode
select u;

我得到这个异常运行它:

{System.ArgumentException: Can't call the method 'MyModel.Activity Find(System.Object[])' declared for type 
'System.Data.Entity.DbSet`1[MyModel.Activity]' as an instance of type 'System.Data.Objects.ObjectQuery`1[MyModel.Activity]'
   en System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method)
   en System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method)
   en System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
   en System.Linq.Expressions.InstanceMethodCallExpressionN.Rewrite(Expression instance, IList`1 args)
   en System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Data.Entity.Internal.Linq.DbQueryVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
   en System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Data.Entity.Internal.Linq.DbQueryVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
   en System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
   en System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
   en System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
   en System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
   en System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
   en System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Data.Entity.Internal.Linq.DbQueryVisitor.VisitMethodCall(MethodCallExpression node)
   en System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   en System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   en System.Data.Entity.Internal.Linq.DbQueryProvider.CreateObjectQuery(Expression expression)
   en System.Data.Entity.Internal.Linq.DbQueryProvider.CreateQuery[TElement](Expression expression)
   en System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
var users = from u in context.Users.Include("WantsToDo")
where u.WantsToDo.Contains(context.Activities.Find(activityId)) &&
    u.Gender == genderId &&
    u.City == city &&
    u.Country == countryIsoCode
select u;

这里: u.WantsToDo.Contains(context.Activities.Find(activityId)) .

这是我User课:

[DataContract]
public class User
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public bool? Active { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public int? Gender { get; set; }
    [DataMember]
    public string Birthday { get; set; }
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string City { get; set; }
    [DataMember]
    public string Country { get; set; }
    public virtual ICollection<Activity> WantsToDo { get; set; }
}

为什么我会收到该错误?

其中子句使用导航属性例外

这就是我解决问题的方式:

var users = from u in context.Users
from act in u.WantsToDo
where act.ActivityId == activityId &&
    u.Gender == genderId &&
    u.City == city &&
    u.Country == countryIsoCode
select u;

试试这个

var users = from u in context.Users
where u.WantsToDo.Any(e => e.ActivityId == activityId) &&
    u.Gender == genderId &&
    u.City == city &&
    u.Country == countryIsoCode
select u;