{"id":36,"date":"2013-03-06T10:07:00","date_gmt":"2013-03-06T04:37:00","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=36"},"modified":"2013-03-06T10:07:00","modified_gmt":"2013-03-06T04:37:00","slug":"aggregate-and-grouping-functions-using-linq-in-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2013\/03\/aggregate-and-grouping-functions-using-linq-in-crm\/","title":{"rendered":"Aggregate and Grouping functions using LINQ in CRM"},"content":{"rendered":"<div dir=\"ltr\" trbidi=\"on\"><span><\/span><\/p>\n<div><span><span>As you know, QueryExpression are built as an Object Model. They support all the features of FetchXML except for grouping &#038; aggregates.<\/p>\n<p><\/span><\/span><\/div>\n<p><span> <\/span><\/p>\n<div><span><span>FetchXML supports all the features of QueryExpression including grouping &#038; aggregates. Queries here are built as XML statements.<\/p>\n<p><\/span><\/span><\/div>\n<p><span> <\/span><\/p>\n<div><span><span>LINQ queries are built using standard language similar to SQL, but internally it uses QueryExpression and hence it is limited to the features of QueryExppression<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><a href=\"http:\/\/1.bp.blogspot.com\/-4phDc0Tyogs\/UTb1FTaeu9I\/AAAAAAAABn4\/W5r2h90tlII\/s1600\/new.png\" imageanchor=\"1\"><span><img decoding=\"async\" border=\"0\" height=\"130\" src=\"http:\/\/1.bp.blogspot.com\/-4phDc0Tyogs\/UTb1FTaeu9I\/AAAAAAAABn4\/W5r2h90tlII\/s1600\/new.png\" width=\"400\"><\/span><\/a><\/div>\n<p><span><\/span><br \/><span><\/span><\/p>\n<div><span><span>The <b>QueryExpression<\/b>class supports complex queries.<\/p>\n<p><\/span><\/span><\/div>\n<p><span><span>The <b>QueryByAttribute<\/b> class is a simple means to search for entities where attributes matches specified values.<\/p>\n<p><\/span><\/span><br \/><span><\/span><br \/><span><span>When using LINQ, it returns IQueryable<Entity><entity><entity> which is not a collection &#038; hence we can\u2019t do group-by or aggregate.<\/p>\n<p><\/entity><\/entity><\/span><\/span><br \/><span><\/span><br \/><span>IQueryable<Entity> queryDemo = from a in orgServiceContext.AccountSet<\/span><\/p>\n<p><span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\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>where a.Address1_City.Contains(&#8220;a&#8221;)<\/span><\/span><\/p>\n<div><span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 select a;<\/span><\/p>\n<p><span>\u00a0<\/span><\/p>\n<\/div>\n<div><span><\/span>\u00a0<\/div>\n<div><b><span><span>However, LINQ supports group by in following way.<\/p>\n<p><\/span><\/span><\/b><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> Write a simple LINQ Query<\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>var lnqQuery = from o in orgServiceContext.OpportunitySet<\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span>where o.EstimatedValue.Value >= 10000<\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>select new<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>\u00a0\u00a0 {<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span>OpportunityTopic = o.Name,<\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>PotentialCustomer = o.CustomerId,<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> Rating = o.OpportunityRatingCode<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> };<\/span><\/span><br \/><span><span><\/p>\n<p><\/span><\/span>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> In another query pass your created query as a list. You can either do a ToList() or you can make your own List like List<Groups> estList = new List<Groups>() where Groups is your custom class with set &#038; get. <\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span> <\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>Here, we are using ToList() &#038; grouping the collection by Rating.<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>var lnqQuery2 = (from f in lnqQuery.ToList()<\/p>\n<p><\/span><\/span><br \/><span><\/span>\u00a0<\/div>\n<div><span><span> \/\/Pass the lnqQuery as a list using ToList() &#038; then group by<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> group f by f.Rating into queryGrp<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>  let first = queryGrp.First()<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span> \/\/selecting only the first record of all grouped record<\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>select new<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> {<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> Name = first.OpportunityTopic,<\/span><\/span><br \/><span><span><\/p>\n<p><\/span><\/span>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> Rating = first.Rating.Value<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> });<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span> Now if we want to retrieve all the records from the Query Group, then we can simply create a list of query Group using ToList() &#038; iterate through it as shown in below example<\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span> var lnqQuery2 = (from f in lnqQuery.ToList()<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> \/\/Pass the lnqQuery as a list using ToList() &#038; then group by<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>group f by f.Rating into queryGrp<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span> \/\/Make a list of queryGrp using ToList()<\/span><\/span><br \/><span><span><\/p>\n<p><\/span><\/span>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>select queryGrp.ToList());<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>   \/\/Create a DataTable<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>DataTable table = new DataTable();<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span>\/\/Add some columns<\/span><\/p>\n<p>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>table.Columns.Add(&#8220;Topic&#8221;);<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>table.Columns.Add(&#8220;Cust&#8221;);<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>table.Columns.Add(&#8220;Rating&#8221;);<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>foreach (var e1 in lnqQuery2)<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>{<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span>\/\/iterate through each record grouped with rating<\/span><\/p>\n<p>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>foreach (var e2 in e1)<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>{<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span>\/\/iterate through each individual record that belongs in a rating<\/span><br \/><span><\/span><\/p>\n<p>\u00a0<\/p><\/div>\n<p><span><\/span><\/p>\n<div><span><span>DataRow dataRow = table.NewRow();<\/p>\n<p><\/span><\/span><br \/><span><\/span>\u00a0<\/div>\n<div><span><span>dataRow[&#8220;Topic&#8221;] = e2.OpportunityTopic;<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span>dataRow[&#8220;Cust&#8221;] = e2.PotentialCustomer.Name;<\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>dataRow[&#8220;Rating&#8221;] = e2.Rating;<\/p>\n<p><\/span><\/span><br \/>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span>table.Rows.Add(dataRow);<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>\u00a0}<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>}<\/p>\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span>\/\/here you have your list in your dataGridView<\/span><\/p>\n<p>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span>dataGridView1.DataSource = table;<\/span><\/p>\n<p><span><\/span><\/p>\n<p>\u00a0<\/div>\n<p><span><\/span><\/p>\n<div><span><span><b>Note-<\/b> Here we are just showing the rating value. You can get the rating by using FormattedValues[&#8220;opportunityratingcode&#8221;]\n<p><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><span>For more details you can visit <\/span><span><a href=\"http:\/\/msdn.microsoft.com\/en-in\/library\/gg334607.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a><\/span><\/span><\/div>\n<p><span><\/span><\/p>\n<div><span><\/span><br \/><span><span>Hope this article helps!<span><\/p>\n<p><\/span><\/span><\/span><\/div>\n<p><span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\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><\/p>\n<div><span>\u00a0<\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>As you know, QueryExpression are built as an Object Model. They support all the features of FetchXML except for grouping &#038; aggregates. FetchXML supports all the features of QueryExpression including grouping &#038; aggregates. Queries here are built as XML statements. LINQ queries are built using standard language similar to SQL, but internally it uses QueryExpression\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2013\/03\/aggregate-and-grouping-functions-using-linq-in-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[19],"tags":[122,795,876,992,1392],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","tag-aggregate","tag-fetchxml","tag-grouping","tag-linq","tag-queryexpression"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}