Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extension method no longer works
Message
 
 
À
29/03/2013 19:12:17
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01569549
Message ID:
01569568
Vues:
26
>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform