Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Extension method no longer works
Message
 
 
To
29/03/2013 19:12:17
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01569549
Message ID:
01569568
Views:
27
>Anything in SqlMethods is for Linq to Sql queries only (see http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.aspx). You don't really need it anyways since you can use the Contains method of the string class:
>
public static IQueryable< T> Like< T>(this IQueryable< T> source, string propertyName, string keyword)
>{
>	var type = typeof(T);
>	var property = type.GetProperty(propertyName);
>	var parameter = Expression.Parameter(type, "p");
>	var propertyAccess = Expression.MakeMemberAccess(parameter, property);
>	var constant = Expression.Constant(keyword);
>
>	var like = typeof(string).GetMethod("Contains",
>			   new Type[] { typeof(string)});
>	MethodCallExpression methodExp = Expression.Call(propertyAccess, like, constant);
>	Expression< Func< T, bool>> lambda =
>		  Expression.Lambda< Func< T, bool>>(methodExp, parameter);
>	return source.Where(lambda);
>}
Thanks a lot, this works fine.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform