0

我首先使用 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; }
}

为什么我会收到这个错误?

4

2 回答 2

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;
于 2013-10-04T16:10:57.870 回答
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;
于 2013-10-04T16:09:21.337 回答