Editor.Where()

Editor.Where()

Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

Hello,

Since the first parameter for Editor.Where() is column name, I assume that there isn't way to do a condition that doesn't involve column name but only a constant, such as a function input parameter. Or there is a way other than using .Where()?

Thank you,

John Li

Answers

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin

    Hi John,

    You can use the result of an SQL function as part of the query by using a function for the condition but the first parameter would still be a column.

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hey Allan,

    I might have misunderstood your response. But what I intended to do is

    in

     public ActionResult RptDynamicSection(int HwyID)
    {
    Editor.()
     .Field(new Field("Table.HighwayID"))
    
    .Where ()
    }
    
    

    to construct .Where to get the following query condition:

    (HwyID=0)  OR (HwyID>0 AND Table.HighwayID=HwyID)
    
    

    There is no "0" value in the table. By doing above I try to get all the records with "0" value.

    I wonder if there is a solution with using .Where() or anything else.

    Regards,

    John Li

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    public ActionResult RptDynamicSection(int HwyID)
    {
    Editor.()
      .Field(new Field("Table.HighwayID"))
      .Where( q => {
        q.Where( r => {
          r.Where("HwyID", 0);
          r.OrWhere( s => {
            s.Where("HwyID", 0, ">");
            s.Where("Table.HighwayID", "HwyID", "=", false);
          });
      })
    }
    

    Is how that could be done with our database class. The false parameter on the last Where condition just ensures that the value isn't bound - see docs here.

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hello Allan,

    I had tried, and tried again this morning. I kept getting DataTables message "Invalid column name 'HwyID' " . I guess the message makes sense because HwyID is not a table column name.

    I read the linked doc and .NET API doc several times they all state that the first parameter of .Where() is a table name. I must still be missing something. Could you shed more light on this?

    Regards,

    John

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin

    I guess the message makes sense because HwyID is not a table column name.

    What is it then? I took it from your line above.

    The first parameter for Where is the column name, but it can be prefixed by a table name.

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hey Allan,

    Sorry for the confusion. "HwyID" is a parameter in method

    public ActionResult RptDynamicSection(int HwyID)
    
    

    It is just a parameter, not associated with any specific table column.

    Thank you,

    John

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin

    Oh - sorry! I even copy / pasted it in the controller I showed above!

    That makes things much easier then - since it effectively means no query when HwyID == 0, so we can apply a condition on it when it is greater than 0:

    public ActionResult RptDynamicSection(int HwyID)
    {
    Editor.()
      .Field(new Field("Table.HighwayID"))
      .Where( q => {
        if (HwyID > 0) {
            q.Where("Table.HighwayID", HwyID);
        }
      })
    }
    

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hey Allan,

    I was so absorbed to .Where() that I didn't even think of 'if'. Thank you for your patience and kindness.

    Regards,

    Yongqi

Sign In or Register to comment.