Results 1 to 5 of 5

Thread: [RESOLVED] Compute with multiple filters

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] Compute with multiple filters

    Is there a way to use the DataTable.Compute Method with 2 or more filters, i have googled a bit and cant seem to come up with answer?
    If not perhpas there is another way to accomplish

    Thanks for any help and or insight

    Current code Code:
    1. Dim Dt As Decimal = NumericUpDown1.Value
    2.                 dtlist.Rows(i)("Value") = dtsold.Compute("Max(ClosePrice)", "Distance <=" & Dt & "")

    I want something like this

    psuedo code Code:
    1. Dim Dt As Decimal = NumericUpDown1.Value
    2.                 dtlist.Rows(i)("Value") = dtsold.Compute("Max(ClosePrice)", "Distance <=" & Dt & " and "BuildingSize<= MAXGla"")

    in other words DT.Compute(Max(Col1), where Col2<=? and Col3 <=?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compute with multiple filters

    This really comes down to whether you know how to concatenate strings together or not. If you have multiple criteria to filter on then you simply AND or OR them together, just as the documentation says, e.g.
    Code:
    filter = "Column1 = someValue AND Column2 = someOtherValue"
    If you want to use a variable for the values then, exactly as you always do, you concatenate the variable into the String:
    Code:
    filter = "Column1 = " & someValue & " AND Column2 = someOtherValue"
    or use String.Format:
    Code:
    filter = String.Format("Column1 = {0} AND Column2 = someOtherValue", someValue)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Compute with multiple filters

    Quote Originally Posted by jmcilhinney View Post
    This really comes down to whether you know how to concatenate strings together or not. If you have multiple criteria to filter on then you simply AND or OR them together, just as the documentation says, e.g.
    Code:
    filter = "Column1 = someValue AND Column2 = someOtherValue"
    If you want to use a variable for the values then, exactly as you always do, you concatenate the variable into the String:
    Code:
    filter = "Column1 = " & someValue & " AND Column2 = someOtherValue"
    or use String.Format:
    Code:
    filter = String.Format("Column1 = {0} AND Column2 = someOtherValue", someValue)
    thanks that was my exact problem I had an extra "

    btw I couldnt find how to use multiple filter in the documentation, just curious if you have a link to it, I had looked there first

    Thanks again

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compute with multiple filters

    Quote Originally Posted by billboy View Post
    I couldnt find how to use multiple filter in the documentation, just curious if you have a link to it, I had looked there first
    Where exactly did you look? This is from the documentation for the DataTable.Compute method:
    The second parameter, filter, determines which rows are used in the expression. For example, if the table contains a date column named "colDate", you could limit the rows with the following expression:

    colDate > 1/1/99 AND colDate < 17/1/99
    It also says this:
    For rules on creating expressions for both parameters, see the DataColumn.Expression property.
    If you follow that link you get to a detailed topic that includes this:
    Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use parentheses to group clauses and force precedence. The AND operator has precedence over other operators. For example:

    (LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'
    Seriously, where did you look?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: [RESOLVED] Compute with multiple filters

    LOL

    I looked right there, that is how I even thought it could be done and why I was trying to use the AND, and how I learned to create an expression

    vb Code:
    1. dtsold.Compute("Max(ClosePrice)", "Distance <= " & Dt & " And GLAMax <= BuildingSize")

    "GLAMax" is a column expression. What through me off was the quotes and thinking I had to quote the "ColumnName", so had I just followed the Documentation closer I would have solved much sooner.

    The quotes are for the whole filter as a string?

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width