{"id":4963,"date":"2007-12-13T18:53:00","date_gmt":"2007-12-13T18:53:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/12\/13\/linq-cookbook-recipe-11-desktop-search-statistics-doug-rothaus\/"},"modified":"2024-07-05T14:36:50","modified_gmt":"2024-07-05T21:36:50","slug":"linq-cookbook-recipe-11-desktop-search-statistics-doug-rothaus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/linq-cookbook-recipe-11-desktop-search-statistics-doug-rothaus\/","title":{"rendered":"LINQ Cookbook, Recipe 11: Desktop Search Statistics (Doug Rothaus)"},"content":{"rendered":"<h2><b><span><font face=\"Calibri\">Ingredients:<\/font><\/span><\/b><\/p>\n<p class=\"MsoListParagraph\"><span><span><br \/>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\"><span>Visual Studio 2008 (Beta2 or Higher)<\/span><span><\/p>\n<p><\/span><\/font><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><b><span>Categories:<\/span><\/b><span> LINQ to DataSet<\/p>\n<p><\/span><\/font><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><font face=\"Calibri\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\">Introduction:<\/p>\n<p><\/font><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">You can use aggregate functions in LINQ queries to perform calculations on sets of data. Visual Basic includes a set of standard aggregate functions for LINQ queries: <b>All<\/b>, <b>Any<\/b>, <b>Average<\/b>, <b>Count<\/b>, <b>LongCount<\/b>, <b>Max<\/b>, <b>Min<\/b>, <b>Sum<\/b>. These functions are documented in the reference topics for the <\/font><\/span><a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/bb531251(VS.90).aspx\"><span><font face=\"Calibri\">Aggregate Clause<\/font><\/span><\/a><span><font face=\"Calibri\">. You can use aggregate functions as part of the <b>Aggregate<\/b> clause for an entire set of data, or as part of the <b>Into<\/b> portion of a <b>Group By<\/b> or <b>Group Join<\/b> clause, where the aggregate function will be applied to each group of data as shown in the following example.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> customersByCountry = <span>From<\/span> cust <span>In<\/span> customers _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Order<\/span> <span>By<\/span> cust.City _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Group By<\/span> CountryName = cust.Country _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Into<\/span> RegionalCustomers = <span>Group<\/span>, Count() _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Order<\/span> <span>By<\/span> CountryName<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">In this recipe, you will create a Windows Forms application that queries for items indexed using <\/font><\/span><a href=\"http:\/\/www.microsoft.com\/windows\/products\/winfamily\/desktopsearch\/default.mspx\"><span><font face=\"Calibri\">Windows Desktop Search<\/font><\/span><\/a><span><font face=\"Calibri\">. The application returns the t\notal count and total kilobytes for items that are documents, e-mails, or images. The application uses the OLEDB provider for Windows Desktop Search to retrieve item information and LINQ to DataSet to group the item information and apply the <b>Count<\/b> and <b>Sum<\/b> aggregate functions.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\">Instructions:<\/font><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><\/p>\n<p><\/font><\/span><\/b>&nbsp;<\/p>\n<p class=\"MsoListParagraphCxSpFirst\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">Create a Windows Forms Application.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">From the <b><span>Toolbox<\/span><\/b>, drag a <b>ListBox<\/b> control, and a <b>DataGridView<\/b> control onto the form. Resize the form and controls as needed.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">Select the <b>ListBox<\/b> control. In the <b><span>Properties<\/span><\/b> page, locate the <b>Items<\/b> property and click the ellipsis (&#8230;) button to add items to the <b>ListBox<\/b>. Add the following three items and click <b><span>OK<\/span><\/b>:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\"><span><span>o<span>&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">Document<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\"><span><span>o<span>&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">E-mail<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\"><span><span>o<span>&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">Image<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoListParagraphCxSpLast\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font face=\"Calibri\">Double-click the <b>ListBox<\/b> control to edit the <b>SelectedIndexChanged<\/b> event and add the following code:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8216; Connect to the Desktop Search OLEDB provider. Return a list of items and the size<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8216; of each item where the item type contains the search string selected from the ListBox.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> conn <span>As<\/span> <span>New<\/span> OleDbConnection(<span>&#8220;Provider=Search.CollatorDSO.1;&#8221;<\/span> &amp; _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8220;Persist Security Info=False;&#8221;<\/span> &amp; _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8220;Extended Properties=&#8217;Application=Windows'&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> cmd <span>As<\/span> <span>New<\/span> OleDbCommand(<span>&#8220;SELECT System.ItemTypeText, System.Size &#8220;<\/span> &amp; _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8220;FROM SystemIndex WHERE &#8220;<\/span> &amp; _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8220;CONTAINS(System.ItemTypeText, &#8216;&#8221;&#8221;&#8221;<\/span> &amp; ListBox1.SelectedItem &amp; <span>&#8220;&#8221;&#8221;&#8216;)&#8221;<\/span>, conn)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> adapter <span>As<\/span> <span>New<\/span> OleDbDataAdapter(cmd)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> ds <span>As<\/span> <span>New<\/span> DataSet<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>adapter.Fill(ds, <span>&#8220;SearchResults&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8216; Group the results from the Desktop Search. Count the number of items in each <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8216; group and sum the size of the items in each group.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> query = <span>From<\/span> row <span>In<\/span> ds.Tables(<span>&#8220;SearchResults&#8221;<\/span>) _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Let<\/span> ItemTypeText = row(<span>&#8220;System.ItemTypeText&#8221;<\/span>), _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>Size = <span>CInt<\/span>(row(<span>&#8220;System.Size&#8221;<\/span>)) _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Group<\/span> <span>By<\/span> ItemTypeText <span>Into<\/span> TotalItems = Count(), Bytes = Sum(Size) _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Select<\/span> Type = ItemTypeText, Count = TotalItems, _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Size = (Bytes \/ 1000).ToString(<span>&#8220;#,###&#8221;<\/span>) _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Order<\/span> <span>By<\/span> Type<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8216; Display the grouped results.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>DataGridView1.DataSource = query.ToList()<\/span><\/p>\n<p class=\"MsoNormal\"><span><span><\/p>\n<p><\/span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Press <b>F5<\/b> to see <span>the<\/span> code run. Click on the different search terms in the <b>ListBox<\/b> to see the grouped results including the count of each item and the size (in kilobytes) of each item group.<\/font><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Ingredients: &middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio 2008 (Beta2 or Higher) &nbsp; Categories: LINQ to DataSet &nbsp; Introduction: You can use aggregate functions in LINQ queries to perform calculations on sets of data. Visual Basic includes a set of standard aggregate functions for LINQ queries: All, Any, Average, Count, LongCount, Max, Min, Sum. These functions are documented in [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[59,93,94,117,166],"class_list":["post-4963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-doug-rothaus","tag-linq-cookbook","tag-linqvb9","tag-orcas","tag-vb2008"],"acf":[],"blog_post_summary":"<p>Ingredients: &middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio 2008 (Beta2 or Higher) &nbsp; Categories: LINQ to DataSet &nbsp; Introduction: You can use aggregate functions in LINQ queries to perform calculations on sets of data. Visual Basic includes a set of standard aggregate functions for LINQ queries: All, Any, Average, Count, LongCount, Max, Min, Sum. These functions are documented in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}