{"id":121,"date":"2013-03-26T17:00:49","date_gmt":"2013-03-26T22:00:49","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=121"},"modified":"2013-03-26T17:00:49","modified_gmt":"2013-03-26T22:00:49","slug":"datatable-compute-method","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/03\/26\/datatable-compute-method\/","title":{"rendered":"DataTable Compute Method"},"content":{"rendered":"<div><\/div>\n<table width=\"100%\" border=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">Compute is one of the methods of DataTable class. This method computes an expression on the current rows that pass the filter criteria.<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\"><span style=\"text-decoration: underline;\">Syntax<\/span><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\"><strong>Object DataTable.Compute(<\/strong>string expression , string filter<strong>)<\/strong><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\"><strong>Expression<\/strong>\u00a0&#8211; The expression to compute.<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\"><strong>Filter<\/strong>\u00a0&#8211; The filter to limit the rows that evaluate in the expression.<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">This method&#8217;s return type is\u00a0<strong>System.Object<\/strong>. So, you have to cast it with appropriate data type.<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">The following operations can be passed through as expression parameter.<\/span><\/div>\n<div><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Sum -&gt; Sum<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Average -&gt; Avg<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Minimum -&gt; Min<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Maximum -&gt; Max<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Count -&gt; Count<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Statistical standard deviation -&gt;StDev<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Statistical variance -&gt; Var<\/strong><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\"><span style=\"text-decoration: underline;\">Example:<\/span><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>DataTable dtTotal = new DataTable();<\/strong><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">Suppose your DataTable dtTotal contain two columns as<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>Name\u00a0\u00a0\u00a0\u00a0\u00a0 Price<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0y\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 50<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0z\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0u\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 25<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0y\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 15<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 35<\/strong><\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>\u00a0u\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20<\/strong><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">If you want to count the total price of distinct names, then you can use compute method like below statement.<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: 'courier new', courier; font-size: small;\"><strong>int total = dtTotal.Compute(&#8220;Sum ( Price ) &#8220;, &#8220;Name = &#8216;x'&#8221;);<\/strong><\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">Output &#8211; 80<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/div>\n<div><span style=\"font-family: verdana, geneva; font-size: small;\">This method looks pretty simple, but sometimes its SQL like Aggregate Function functionality solves complex calculations.<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>http:\/\/www.mindfiresolutions.com\/DataTable-Compute-Method-628.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compute is one of the methods of DataTable class. This method computes an expression on the current rows that pass the filter criteria. \u00a0 Syntax&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-vb-net"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}