locked
Initialize a variable with Query RRS feed

  • Question

  • User-735851359 posted

    Hello,

    How can I initialize a variable that will contain a Query?

        var sqlQ = "";
        var info = ?????; 
        if(IsPost){
            if(Request.Form["urunler"]=="filtrele"){
                var cid = Request["compid"];
                sqlQ = "SELECT .....";
                info = data.Query(sqlQ, cid);
                grid = new WebGrid(source: info, rowsPerPage: 50);
            }
            
        }else{...

    Thanx in advance

    Thursday, May 1, 2014 6:27 AM

Answers

  • User938738290 posted

    I think that what OP needs is how to declare a variable that contains the result of a query, not a string that holds the query itself.

    For the record, Ayanmesut, I encourage you to type out your variable types rather than relying on the "var" keyword.  Those three letters have a nasty tendency to turn perfectly competent programmers into grunting apes.  Also, forcing yourself to understand what types are returned by library functions (Like, say, Database.Query()) will help you better understand the mechanism of those functions.

    In any case, Database.Query() returns an IEnumerable<dynamic>.  (Dynamic types are a HUGE subject that I can't possibly cover in much depth here, please see this link for more info: http://msdn.microsoft.com/en-us/library/dd264736.aspx)

    IEnumerable<dynamic> lResult = mDatabase.Query("select * from MY_TABLE where MY_TABLE.MY_COLUMN = @0", 42);

    If the query requires parameters, say, from the Request, and you may or may not be able to run the query, you can do the following:

    IEnumerable<dynamic> lResult;
    if (lSomeParam != null)
    {
        lResult = Database.Query("Select * from MY_TABLE where MY_TABLE.MY_COLUMN = @0", lSomeParam);
    }
    if (lResult != null && lResult.Any())
    {
        // Query returned results, so perform operations on them.
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 1, 2014 9:31 AM

All replies

  • User-1353043493 posted

    Provide some more details. so i will help you

    Thursday, May 1, 2014 8:08 AM
  • User281315223 posted

    Could you provide a bit more detail?

    The code that you have should work if you initially construct a string and then "build" your query within it prior to executing :

    // Store an empty string for your Query
    var sqlQ = "";
    
    // If a Post occurred
    if(IsPost){
            // Check if you are filtering?
            if(Request.Form["urunler"]=="filtrele"){
                var cid = Request["compid"];
                // Adjust your query including a parameter
                sqlQ = "SELECT * FROM YourTable WHERE CompId = @0";
                // Execute your query and populate your grid
                info = data.Query(sqlQ, cid);
                grid = new WebGrid(source: info, rowsPerPage: 50);
            }
            else
            {
                // Do something else here (perhaps something similar to your else clause below
            }
    }
    else
    {
        // Otherwise execute an initial query
        sqlQ = "SELECT * FROM YourTable";
        // Execute your query and populate your grid
        info = data.Query(sqlQ);
        grid = new WebGrid(source: info, rowsPerPage: 50);
    }

    Thursday, May 1, 2014 8:26 AM
  • User938738290 posted

    I think that what OP needs is how to declare a variable that contains the result of a query, not a string that holds the query itself.

    For the record, Ayanmesut, I encourage you to type out your variable types rather than relying on the "var" keyword.  Those three letters have a nasty tendency to turn perfectly competent programmers into grunting apes.  Also, forcing yourself to understand what types are returned by library functions (Like, say, Database.Query()) will help you better understand the mechanism of those functions.

    In any case, Database.Query() returns an IEnumerable<dynamic>.  (Dynamic types are a HUGE subject that I can't possibly cover in much depth here, please see this link for more info: http://msdn.microsoft.com/en-us/library/dd264736.aspx)

    IEnumerable<dynamic> lResult = mDatabase.Query("select * from MY_TABLE where MY_TABLE.MY_COLUMN = @0", 42);

    If the query requires parameters, say, from the Request, and you may or may not be able to run the query, you can do the following:

    IEnumerable<dynamic> lResult;
    if (lSomeParam != null)
    {
        lResult = Database.Query("Select * from MY_TABLE where MY_TABLE.MY_COLUMN = @0", lSomeParam);
    }
    if (lResult != null && lResult.Any())
    {
        // Query returned results, so perform operations on them.
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 1, 2014 9:31 AM