Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

StartWith method in Mark DLinq DynamicQueryExtension class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 22 Jan 2008 23:27 GMT
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));
}
Nicholas Paldino [.NET/C# MVP] - 23 Jan 2008 03:22 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I tried to create StartsWith extension method for Mark
>DynamicQueryExtension
[quoted text clipped - 40 lines]
> return query.Where(Expression.Lambda<Func<TEntity,bool>>(testExp, param));
> }
Andrus - 23 Jan 2008 13:39 GMT
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.
Andrus - 23 Jan 2008 18:24 GMT
>    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));
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.