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));
}
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));
}