ambiguous match exception in Marc Dynamic Query order method


A

Andrus

My entity contains properties which are different only by case.

I tried to use Marc Dynamic Order but in this case line in method
UtilityOrderBy<T>()

MemberExpression member = Expression.Property(param, propertyName);

causes AmbiguousMatchException

How to change method below so that
Expression.Property(param, propertyName)

does not ignore case ?

Should I get PropertyInfo from T and use Property() overload with takes
PropertyInfo parameter ?

Andrus.

static IOrderedQueryable<T> UtilityOrderBy<T>(
IQueryable<T> query, string propertyName, MethodInfo method) {
// crete a property-getter expression
ParameterExpression param = Expression.Parameter(typeof(T), "p");

// this causes Ambiguous match exception :
MemberExpression member = Expression.Property(param, propertyName);
object[] reflArgs = { query, member, param };
// invoke the specified method (for the appropriatePropertyType)
return (IOrderedQueryable<T>)method.MakeGenericMethod(
typeof(T), ((PropertyInfo)member.Member).PropertyType)
.Invoke(null, reflArgs);
}
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

My entity contains properties which are different only by case.

Ick. That's really nasty. Is there any way to avoid it? It's a recipe
for bugs, IMO.
I tried to use Marc Dynamic Order but in this case line in method
UtilityOrderBy<T>()

MemberExpression member = Expression.Property(param, propertyName);

causes AmbiguousMatchException

How to change method below so that
 Expression.Property(param, propertyName)

 does not ignore case ?

Use the overload of Expression.Property which takes a PropertyInfo.
Find the PropertyInfo by using Type.GetProperty on the appropriate
type.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top