I would wish to make a “generic” function allowing to manage the character “%” in my research.
It is necessary that this function is generic because it can apply to any column.
Moreover, it is not sensitive case.
With this intention, I have to create this one:
Code: Select all
static IQueryable CritereString(IQueryable list, Func func, string value)
{
if (value.StartsWith("%") && value.EndsWith("%"))
{
value = value.Substring(1, value.Length - 2);
return list.Where(rp => func(rp).ToUpper().Contains(value));
}
if (value.StartsWith("%"))
{
value = value.Substring(1, value.Length - 1);
return list.Where(rp => func(rp).ToUpper().EndsWith(value));
}
if (value.EndsWith("%"))
{
value = value.Substring(0, value.Length - 1);
return list.Where(rp => func(rp).ToUpper().StartsWith(value));
}
return list.Where(rp => func(rp).ToUpper() == value);
}
Code: Select all
...
var list = Entities.Patients.AsQueryable();
list = CritereString(list, rp => rp.Nom, (string)critere.Value);
...