2016-10-31 3 views
1

Ich habe Methode und Abfrage wie folgt aus:Filter hinzufügen XML

public static string GetChartEnergy(string initDate, string endDate, string type) 
     { 
structure.Add(initDate.CreateQueryStructure(endDate, true, null, "convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103)", null, false)); 
structure.Add(type.CreateQueryStructure(string.Empty, false, "CASE WHEN m.Type = 1 THEN 'Agua' ELSE CASE WHEN m.Type = 2 THEN 'Luz' ELSE 'Gas' END END AS Type", " m.type", "m.Type", false)); 
     } 

CreateQueryStructure ist diese:

public static QueryStructure CreateQueryStructure(this String value, string endDate, bool isDate, 
      string columnName, string whereName, string groupByName, bool isNullField) 
     { 
      QueryStructure structure = new QueryStructure(); 

      if (!string.IsNullOrEmpty(value)) 
      { 
       if (value != ",") 
       { 
        if (isDate) 
        { 
         //obtiene la estructura para un filtro entre fechas 
         structure.ColumnSelect = columnName; 
         structure.ColumnGroupBy = groupByName; 
         structure.ColumnWhere = string.Format("({0} BETWEEN convert(datetime,\'{1}\', 103) and convert(datetime,\'{2}\', 103))", whereName, value.Remove(value.Length - 1), endDate.Remove(value.Length - 1)); 
         structure.Values = null; 
         structure.Operator = Operator.Nothing; 
        } 
        else 
        { 
         if (isNullField) 
         { 
          //obtiene la estructura de un filtro por un campo que es null o no 
          if (value.Remove(value.Length - 1) != "-1") 
          { 
           structure.ColumnWhere = string.Format("{0} IS{1} NULL", whereName, 
            value.Remove(value.Length - 1) == "0" 
                     ? " NOT" : 
                     string.Empty); 

           structure.Values = null; 
           structure.Operator = Operator.And; 
          } 
         } 
         else 
         { 
          //obtiene la estructura de un campo aplicando la regla IN seleccionando 
          //el campo a mostrar y el campo en groupBy 
          structure.ColumnSelect = columnName; 
          structure.ColumnGroupBy = groupByName; 
          structure.ColumnWhere = whereName; 
          structure.Values = value.Remove(value.Length - 1); 
          structure.Operator = Operator.And; 
         } 
        } 
       } 
      } 

      return structure; 
     } 

OUTPUT: "(convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103) BETWEEN convert(datetime,'01/01/2014', 103) and convert(datetime,'31/10/2016', 103))"

Nun, wenn es von zwei CreateQueryStructure passieren Ich habe eine andere Methode wie:

public static string GetChartInfo(List<QueryStructure> queryStructure, string procedureName) 
     { 
       var queryWhere = queryStructure.GetWhere(); 
} 

So gibt es jetzt GetWhere:

public static string GetWhere(this List<QueryStructure> filters) 
     { 
      string result = string.Empty; 

      if (filters != null && filters.Count > 0) 
      { 
       if (filters.Select(x => x.ColumnWhere).Any()) 
       { 
        result += "WHERE "; 

        foreach (var filter in filters) 
        { 
         if (filter.Operator != Operator.Nothing) 
         { 
          result += " " + filter.Operator.ToString() + " "; 
         } 

         if (!string.IsNullOrEmpty(filter.Values)) 
         { 
          result += filter.ColumnWhere + " IN ("; 

          result += filter.Values; 

          result += ") "; 

         } 
         else 
         { 
          result += filter.ColumnWhere; 
         } 
        } 
       } 
      } 

      return result; 
     } 

Und es endlich wieder Output mit where-Klausel:

"WHERE (convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103) BETWEEN convert(datetime,'01/01/2014', 103) and convert(datetime,'31/10/2016', 103)) And m.type IN (2) " 

Und ich will Klausel ändern, in dem nur die Artikel von current zu bekommen, so dass ich current in Methode sendet Parameter von der Steuerung wie:

public static string GetChartEnergy(string initDate, string endDate, string type, int currentUser) //there I have currentUser 

Nun, wie kann ich hinzufügen, um diesen Filter:

structure.Add(initDate.CreateQueryStructure(endDate, true, null, "convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103)", null, false)); 

in Linq Ich brauche nur einige, wie x => x.user == currentUser zu tun, aber in xml Ich weiß nicht, wie kann jemand mir helfen ?. Grüße

Update: Abfrage Strukturklasse:

public class QueryStructure 
    { 
     public string ColumnGroupBy { get; set; } 

     public string ColumnSelect { get; set; } 

     public string ColumnWhere { get; set; } 
     public Operator Operator { get; set; } 

     public string Values { get; set; } 

    } 
+0

Können Sie bitte erläutern, was Ihre Eingabe vs Ausgabe der obigen Funktion ist, auch wird es gut sein, wenn Sie QueryStructure-Klasse einfügen. –

+0

Ich restrukturiere meine Frage, um klarer zu sein @DirtyDeveloper – Dawin

+0

können Sie bitte auch die QueryStructure-Modell-Klasse einfügen –

Antwort

1

auf Ihren Methoden der Suche wird es sehr einfach:

public static string GetChartEnergy(string initDate, string endDate, string type,string currentUser) 
    { 
structure.Add(initDate.CreateQueryStructure(endDate, true, null, "convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103)", null, false)); 
structure.Add(type.CreateQueryStructure(string.Empty, false, "CASE WHEN m.Type = 1 THEN 'Agua' ELSE CASE WHEN m.Type = 2 THEN 'Luz' ELSE 'Gas' END END AS Type", " m.type", "m.Type", false)); 

structure.Add(curentUser.CreateQueryStructure(string.Empty, false, string.Empty, "m.User", string.Empty, false)); 
} 

vorausgesetzt m.User der Benutzer Spalte zu filtern, die geben Sie den Ausgang wie folgt aus

WHERE (convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103) BETWEEN convert(datetime,'01/01/2014', 103) and convert(datetime,'31/10/2016', 103)) And m.type IN (2) AND m.User IN("yourusername") 
+0

@Dawin, lassen Sie mich wissen, wenn Sie dagegen laufen Ihren Code und lassen Sie mich wissen, wenn Sie Änderungen benötigen –

+0

Natürlich teste ich es morgen und ich rate Ihnen, wenn es funktioniert.Vielen Dank ! Grüße – Dawin

+0

Dreckig bist du da? – Dawin