StartWith method in Mark DLinq DynamicQueryExtension class

A

Andrus

I tried to create StartsWith extension method for Mark DynamicQueryExtension
class published earlier.

I got compile error shown in comment.

How to fix ?

Andrus.


/// <summary>
/// Returns all items whose property name starts with value, case
insensitive.
///
/// Sample:
/// var q = db.Products.StartsWith( "ProductName", "P" )
///
/// Executes query
/// var q = from p in db.Products
/// where p.ProductName.ToUpper().StartsWith(value.ToUpper(), true,
/// System.Globalization.CultureInfo.CurrentCulture)
/// select p;

/// <param name="propertyName">entity property name, must be string
property</param>

/// <param name="value">value with the property value starts</param>

///
/// </summary>

public static IQueryable<TEntity> StartsWith<TEntity>(this
IQueryable<TEntity> query, string propertyName, string value) {

ParameterExpression param = Expression.Parameter(typeof(TEntity), "p");
// 'System.Linq.Expressions.LambdaExpression' does not contain a
// definition for 'StartsWith'
BinaryExpression testExp = LambdaExpression.StartsWith(
Expression.Property(param, propertyName),
Expression.Constant(value.ToUpper(), typeof(string))
);

return query.Where(Expression.Lambda<Func<TEntity,bool>>(testExp, param));
}
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

Well, the error is pretty explicit. You are trying to call the static
StartsWith method on the LambdaExpression class. However, that method
doesn't exist.
 
A

Andrus

Nicholas,
Well, the error is pretty explicit. You are trying to call the static
StartsWith method on the LambdaExpression class. However, that method
doesn't exist.

Thank you.
Do you have any idea how to fix this code?

I want Dynamic Linq to force generation of parameterized query

ExecuteCommand( "select * FROM Products WHERE %0 ILIKE %1 || '%' ",
columnName, startOfValue );

In this case server uses index if this exists.

Andrus.
 
A

Andrus

Well, the error is pretty explicit. You are trying to call the static
StartsWith method on the LambdaExpression class. However, that method
doesn't exist.

I think I need to create expression

p.&propertyName.StartsWith(&value,
System.StringComparison.CurrentCultureIgnoreCase)

I tried the code below got got runtime error

No method 'StartsWith' on type 'System.String' is compatible with the
supplied arguments.

StartsWith( string, System.StringComparison ) method exists in .NET

How to fix this error ?


public static IQueryable<TEntity> StartsWith<TEntity>(this
IQueryable<TEntity> query, string propertyName, string value) {

ParameterExpression param = Expression.Parameter(typeof(TEntity), "p");

//Error: No method 'StartsWith' on type 'System.String' is compatible with
the supplied arguments.

MethodCallExpression testExp = LambdaExpression.Call(
Expression.Property(param, propertyName),
"StartsWith",
new Type[] { typeof(string), typeof(System.StringComparison) },
new Expression[] { Expression.Constant(value),
Expression.Constant(System.StringComparison.CurrentCultureIgnoreCase) });

return query.Where(Expression.Lambda<Func<TEntity, bool>>(testExp, param));
}
 

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