So I want to make a general sorter for my data. I have this code to get data from the database which will extract the data only which contains value
.
using System.Linq.Dynamic;
public static IQueryable<object> SortList(string searchString, Type modelType,
IQueryable<object> model)
{
....
string toStringPredicate = type == typeof(string) ? propertyName +
".Contains(@0)" : propertyName + ".ToString().Contains(@0)";
model = model.Where(propertyName + " != NULL AND " + toStringPredicate, value);
}
The model is this:
public class ManageSubscriberItems
{
public int? UserId { get; set; }
public string Email { get; set; }
public Guid SubscriberId { get; set; }
}
When I call:
models = (IQueryable<ManageSubscriberItems>)EcommerceCMS.Helpers.FilterHelper
.SortList(searchString, typeof(ManageSubscriberItems), models);
if(models.Any())
It throws this error:
"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."
EDIT
I found the problem, but I still cannot fix it. So if the property is not string
, it will throw an error when calling .ToString().Contains()
.
model = model.Where(propertyName + " != NULL AND " + propertyName +
".ToString().Contains(@0)", value);
What I want is to implement LIKE
in the query. Can anyone help me?
ToString
seems to work for me in LINQ to SQL statically or dynamically.SqlFunctions.StringConvert
instead.IQueryable
form. Probably it will work if I calledToList()
orToEnumerable()
first, but I do not want to process this in the memory. I tried usingSqlFunctions.StringConvert
in the dynamic expression (written in string) and it gives me an error. Right now I can only check if it is numeric, I use==
, if it is string I use.Contains()
. This is not what I want. It is not behaving likeLike
operator.