Friday, March 18, 2016

Groupby and Count



HttpCookie recentSearchCoookie = HttpContext.Current.Request.Cookies[FLIGHT_QUERY_MODEL_RECENT_COOKIE];
            var oldCookieValue = (recentSearchCoookie != null) ? HttpUtility.UrlDecode(recentSearchCoookie.Value) : null;
            var oldCookieObjectCollection = (oldCookieValue != null) ? JsonConvert.DeserializeObject<List<FlightSearchModel>>(oldCookieValue) : null;

            // Comapre to values set on rule (properties above)
            if (oldCookieObjectCollection != null)
            {
                var groupsByDestination = oldCookieObjectCollection.GroupBy(info => info.FlightSearchSegmentList[0].DestinationCityCode)
                        .Select(group => new
                        {
                            Destination = group.Key,
                            Count = group.Count()
                        })
                        .OrderBy(x => x.Count);

var groupsByDestinationAndOrigin = oldCookieObjectCollection.GroupBy(info => new { info.FlightSearchSegmentList[0].OriginCityCode, info.FlightSearchSegmentList[0].DestinationCityCode })
                        .Select(group => new
                        {
                            Route = group.Key,
                            Count = group.Count()
                        })

                        .OrderBy(x => x.Count);
            }


            return true;

No comments:

Post a Comment