Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extension method no longer works
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Extension method no longer works
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01569549
Message ID:
01569549
Vues:
50
Hi everybody,

I have this complex extension method
 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(SqlMethods).GetMethod("like",
                      new Type[] { typeof(string), typeof(string) });
           MethodCallExpression methodExp =
                 Expression.Call(null, like, propertyAccess, constant);
           Expression<Func<T, bool>> lambda =
                 Expression.Lambda<Func<T, bool>>(methodExp, parameter);
           return source.Where(lambda);
        }
which is applied in the following context:
 var clients = Db.Clients; // Should set type of clients to IQueryable<Clients>
....

if (!string.IsNullOrEmpty(qtype) && !string.IsNullOrEmpty(query))
            {
                clients = clients.Like(qtype, query);
            }

            int Total = clients.Count()
The last line now generates this error:

LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.

I am not sure how to fix the problem. I assume I need to somehow convert my Db.Clients to be of type LinqToSql instead of EF.

Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform