Pregunta

¿Alguien sabe lo que está mal con mi sintaxis aquí?Estoy haciendo preguntas Dinámicas ESQL y estoy corriendo en un error al intentar hacer donde la condición para el tipo de datos de DateTime.Este es el error:

La sintaxis de la consulta no es válida.Cerca del término '2011', línea 1, columna 135.

Si importa el tipo DateTime en mi entidad, ¿es realmente anulable date de tiempo?

Sin embargo, pensé que esta es la sintaxis correcta de todo lo que he leído.

Aquí está el código:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>()
        {
            new EntityFilter<StudyFirstRead> { PropertyName = "username", OpType = ExpressionType.Equal, Value = "cwoodhouse" },
            new EntityFilter<StudyFirstRead> { PropertyName = "FirstRead", OpType = ExpressionType.LessThan, Value = "DATETIME'2011-02-01 00:00'" }
        };

Donde entityFilter es:

  public class EntityFilter<T>
{
    public string PropertyName { get; set; }
    public ExpressionType OpType { get; set; }
    public object Value { get; set; }

y estoy construyendo consultas dinámicas así:

StringBuilder builder = new StringBuilder();

        int counter = 0;

        string baseStr = @"SELECT VALUE val FROM " + contextName + "." + tableName + " AS val WHERE val.";

        builder.Append(baseStr); 

        foreach (EntityFilter<T> filter in filters)
        {
            //builder.Append(filter.PropertyName + " " + filter.OpTypeString() + " @p" + counter);
            builder.Append(filter.PropertyName + " " + filter.OpTypeString() + "'" + filter.Value + "'"); 
            counter++;

            if (counter < filters.Count)
                builder.Append(" AND val."); 
            else
            {
                break; 
            }
        }

        return builder.ToString(); 

¿Fue útil?

Solución

It actually is the correct syntax for entity SQL (different than regular SQL or T-SQL).

Turns out the problem was too many single quotes, because I had them in both the EntityFilter object and the method that built the dynamic query.

Otros consejos

according to you code the end of your SQL statement would produce something like

AND val.FirstRead = 'DATETIME'2011-02-01 00:00''

when executed you will get error

Error 102: Incorrect syntax near '2011'.

obviously that is not syntactically correct SQL, the quick fix whould be to have your filters collection as such:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>() {
    new EntityFilter<StudyFirstRead> {
        PropertyName = "username",
        OpType = ExpressionType.Equal,
        Value = "cwoodhouse"
    }, new EntityFilter<StudyFirstRead> {
        PropertyName = "FirstRead",
        OpType = ExpressionType.LessThan,
        Value = "2011-02-01 00:00"
    }
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top