Usar expresiones para realizar operaciones de cadena (contiene, comienza con, ...)

I'm trying to make a generic filter for data overviews and I've come across expressions. In that example, some expressions are used to do comparisons:

return Expression.Equal(left, right);
return Expression.GreaterThan(left, right);
return Expression.GreaterThanOrEqual(left, right);
return Expression.LessThan(left, right);
return Expression.LessThanOrEqual(left, right);
return Expression.NotEqual(left, right);

For dates and integer values they are all I need, but when it comes to strings, users will need other filter functionality. So I'd like to add "contains and startswith" possibilities. But looking at "Expression.", it doesn't give me that possibility. I was gonna add some custom Expressions but I cannot find anything relevant to that. The expression returned are used in this piece of code:

Expression.Call(typeof (Queryable),"Where",new[] {list.ElementType},list.Expression,Expression.Lambda<Func<T, bool>>(exp, new[] {pe}));

Does someone has any experience doing this?

Gracias de antemano.

preguntado el 08 de noviembre de 11 a las 10:11

2 Respuestas

Contains y StartsWith are not operator they are simply methods of the class String usarías Experssion.Call to include the use of these.

respondido 08 nov., 11:16

You do not have to add custom Expressions. Just write the dynamic linq expression in the Where method of the object which implements IQueryable.

Me gusta:

...
using System.Linq.Dynamic;
...

string searchKeyword = "ant";
IQueryable<Person> collection = GetPersonCollection();
collection.Where(string.Format("FirstName.Contains('{0}')"), searchKeyword);

Respondido el 04 de enero de 12 a las 18:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.