Where子句使用导航属性异常

时间:2013-10-04 15:43:13

标签: c# linq entity-framework

我正在首先使用C#,.NET Framework 4.0和Entity Framework代码开发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; }
}

为什么我会收到该错误?

2 个答案:

答案 0 :(得分:1)

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

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;

答案 1 :(得分:0)

试试这个

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;