Attention: We are retiring the ASP.NET Community Blogs. Learn more >

DataTable.Compute - the filter parameter

Following on from my original posting about using the Compute method of the DataTable to execute aggregate queries, I should add that, the 2nd argument is mandatory as per the docco here:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataTableClassComputeTopic.asp

The filter parameter is the equivalent to a WHERE clause that you can use to limit the number of records that are returned to the aggregate expression. The example I gave in my previous posting was a good example of this where I used a LIKE expression in the filter parameter to get a count of persons whose name met a given criteria:

int countOfName = (int) dt.Compute("Count(Name)", "Name Like '" + entryTextBox.Text + "*'") ;

If the filter is not required, you must pass an empty string as the argument:

eldestYear.Text = dt.Compute( "MAX(DateOfBirth)", "" ).ToString() ;
youngestYear.Text = dt.Compute( "MIN(DateOfBirth)", "" ).ToString() ;

18 Comments

  • Pleas post an example of sorting a datatable with the compute method.

  • I am binding a data from xml file to dataset.

    This xml Error_Id column has max value of 10

    when i given this query

    dsMain.Tables(0).Compute("max(Error_Id)", "")

    It should return me 10. But it returning as 9. What is the problem

  • VERY important! how can we select values from datatable like using the where clause. for expml select x from y where i=j
    please help

  • is it possible to use 'distinct' with compute funtion.
    i.e, dt.Compute("distinct(itemid)","")
    if yes, pls give me an example

  • It has been my experience that the first argument of the compute method must be a single expression - i..e, something like "AVG(LEN())" will throw an exception.... Does anyone have experience referencing this method from IEnumerator? i.e., Do you know if you can you replace with a column name derived from an IEnumerator iteration?

  • dt.Compute("distinct(date)","")

    this function will right or not just give me help..........

  • When i am using Compute Method to Sum , Beacause the first row is null ......


    So how can i get rid of this problem....

  • Your link to "my original posting..." is broken. Please fix it. Thank you.

  • Thanks

  • > When i am using Compute Method to Sum,
    > Beacause the first row is null

    Use the filter part of Compute (the second argument) to remove the rows that contains a DbNull in that field.

  • может у кого нить есть ещё информация по этому поводу??

  • We must accept finite disappointment, but we must never lose infinite hope.

    -----------------------------------

  • I am having this result in datatable .
    Jobno wagonno container no desc status
    JB001 WG001 C0001 Steam Cap Big Y
    JB001 WG001 C0001 Steam Cap Small Y
    JB001 WG001 C0001 Nut Bolts Y
    JB001 WG001 C0001 Flange Y
    JB001 WG001 C0001 Manlid Gasket Y
    JB001 WG001 C0001 Insulation Y
    JB001 WG001 C0001 Visible Damage Y

    and I want result as,
    jobno wagon no containerno Steam Cap Big Steam Cap Small Nut Bolts Flange Manlid Gasket Insulation Visible Damage
    JB001 WG001 C0001 Y Y Y Y Y Y Y

  • qQDHOZ Really appreciate you sharing this blog article.Thanks Again. Really Great.

  • Glad to be one of several visitants on this amazing web
    site : D.

  • There several interesting points on time on this page conversely I don’t discover if these
    center to heart. You'll be able to find some validity then again I most certainly will take hold opinion until I look into it further. Great article , thanks so we want more! Included with FeedBurner too

  • Undeniably believe that which you said. Your favorite reason appeared to be on the
    internet that the simplest thing to be aware of. I say to
    you, I definitely find out annoyed while people think about worries that they just don’t know about.
    You managed to hit the nail upon that the top and also defined
    out the whole thing without having side-effects , people can take a signal.
    Will likely be back to locate more. Thanks

  • zbIazv wow, awesome blog article.Thanks Again. Really Great.

Comments have been disabled for this content.