Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Extension method no longer works
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Extension method no longer works
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01569549
Message ID:
01569549
Views:
51
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
Next
Reply
Map
View

Click here to load this message in the networking platform