2

Work on EF,Need to write a custom where

public virtual IEnumerable GetDataByID<TEntity>() where TEntity : class
{
 if (this.Context == null)
 Initialize<TEntity>();
 TContext context = this.Context;
 var result = context.CreateObjectSet<TEntity>().Where(GetExpression<TEntity>(5));
 return result;
}
public Expression GetExpression<TEntity>(int id) where TEntity : class
{
 ParameterExpression e = Expression.Parameter(typeof(TEntity), "e");
 PropertyInfo propInfo = typeof(TEntity).GetProperty(EntityInfo<TEntity>.GetPkFieldName(this.Context));//(KeyPropertyName);
 MemberExpression m = Expression.MakeMemberAccess(e, propInfo);
 ConstantExpression c = Expression.Constant(id, typeof(int));
 BinaryExpression b = Expression.Equal(m, c);
 Expression<Func<TEntity, bool>> lambda = Expression.Lambda<Func<TEntity, bool>>(b, e);
 return lambda;
}

Error:

The best overloaded method match for 'System.Data.Objects.ObjectQuery.Where(string, params System.Data.Objects.ObjectParameter[])' has some invalid arguments

Why my above syntax is not working, how to fix it?

Omar
16.7k10 gold badges53 silver badges69 bronze badges
asked Dec 24, 2012 at 11:39
1
  • 6
    What's the error message? Commented Dec 24, 2012 at 11:40

2 Answers 2

1

The problem is, the result of CreateObjectSet<TEntity> returns an ObjectSet<TEntity> which defines its own Where method which the compiler will attempt to use. The arguments you are providing are not valid for this method, hence the error.

What you want to be using is Linq's Queryable.Where extension method like so:

var predicate = GetExpression<TEntity>(5);
var objectSet = context.CreateObjectSet<TEntity>();
var results = System.Linq.Queryable.Where(objectSet, predicate);
answered Dec 24, 2012 at 12:00
Sign up to request clarification or add additional context in comments.

Comments

1

The best overloaded method match for 'System.Data.Objects.ObjectQuery.Where(string, params System.Data.Objects.ObjectParameter[])' has some invalid arguments

The problem is this line:

var result = context.CreateObjectSet<TEntity>().Where(GetExpression<TEntity>(5));

Infact you're specifing the wrong parameters: ObjectQuery<T>.Where

public ObjectQuery<T> Where(
 string predicate,
 params ObjectParameter[] parameters
)

Infact you should pass a string.

answered Dec 24, 2012 at 11:56

1 Comment

I clicked to downvote before your edit, where you were comparing an entity to an expression. SO seems to have registered the downvote as occurring after the edit (minor glitch?) so I am currently unable to remove it. If you make a small edit I will remove the downvote, as I see nothing wrong with the answer now :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.