private static GreenField.DAL.GF_PERF_TOPLEVELYEAR GetTopLevelYearMinInceptionDateRecord(List <GreenField.DAL.GF_PERF_TOPLEVELYEAR> list)
        {
            if (list == null)
            {
                return(null);
            }

            GreenField.DAL.GF_PERF_TOPLEVELYEAR result = list.FirstOrDefault();
            foreach (GreenField.DAL.GF_PERF_TOPLEVELYEAR item in list)
            {
                DateTime resultInceptionDate;
                DateTime itemInceptionDate;

                if (DateTime.TryParse(item.POR_INCEPTION_DATE, out itemInceptionDate).Equals(false))
                {
                    continue;
                }

                if (DateTime.TryParse(result.POR_INCEPTION_DATE, out resultInceptionDate).Equals(false))
                {
                    continue;
                }

                if (itemInceptionDate <= resultInceptionDate)
                {
                    result = item;
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets performance data for a specific snapshot preference where entity type is benchmark
        /// </summary>
        /// <param name="entity">Dimension service entity instance</param>
        /// <param name="preference">MarketSnapshotPreference object</param>
        /// <returns>MarketSnapshotPerformanceData</returns>
        public static MarketSnapshotPerformanceData GetBenchmarkPerformanceData(DimensionEntities entity, MarketSnapshotPreference preference)
        {
            MarketSnapshotPerformanceData result = new MarketSnapshotPerformanceData();

            try
            {
                List <GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION> benchmarkRecords = entity.GF_PERF_DAILY_ATTRIBUTION
                                                                                   .Where(record => record.NODE_NAME == (preference.EntityNodeType == "Country" ? "Country" : "GICS Level 1") &&
                                                                                          record.AGG_LVL_1 == (preference.EntityNodeType == null ? "Undefined" : preference.EntityNodeValueCode) &&
                                                                                          record.AGG_LVL_1_LONG_NAME == (preference.EntityNodeType == null ? "-" : preference.EntityNodeValueName) &&
                                                                                          record.BM == preference.EntityId.ToUpper() &&
                                                                                          record.BMNAME == preference.EntityName &&
                                                                                          record.TO_DATE != null &&
                                                                                          record.POR_INCEPTION_DATE != null)
                                                                                   .OrderByDescending(record => record.TO_DATE).ToList();

                GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION lastRecord = benchmarkRecords.FirstOrDefault();

                GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION benchmarkRecord = lastRecord != null?GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION>(benchmarkRecords
                                                                                                                                                                   .Where(record => record.TO_DATE == lastRecord.TO_DATE).ToList()) : null;

                result.DateToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_1D * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_1D * Convert.ToDecimal(100)) : null;
                result.WeekToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_1W * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_1W * Convert.ToDecimal(100)) : null;
                result.MonthToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_MTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_MTD * Convert.ToDecimal(100)) : null;
                result.QuarterToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_QTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_QTD * Convert.ToDecimal(100)) : null;
                result.YearToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_YTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100)) : null;

                if (preference.EntityNodeType == null)
                {
                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                  .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                         g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                         g.TO_DATE == "31/12/" + (DateTime.Today.Year - 1).ToString() &&
                                                                                                                                                         g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                         g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                         g.POR_INCEPTION_DATE != null)
                                                                                                                                                  .ToList());

                    result.LastYearReturn = preference.EntityNodeType == null ? (benchmarkLastYearRecord != null
                        ? benchmarkLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;

                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkSecondLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                        .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                               g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                               g.TO_DATE == "31/12/" + (DateTime.Today.Year - 2).ToString() &&
                                                                                                                                                               g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                               g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                               g.POR_INCEPTION_DATE != null)
                                                                                                                                                        .ToList());

                    result.SecondLastYearReturn = preference.EntityNodeType == null ? (benchmarkSecondLastYearRecord != null
                        ? benchmarkSecondLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;

                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkThirdLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                       .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                              g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                              g.TO_DATE == "31/12/" + (DateTime.Today.Year - 3).ToString() &&
                                                                                                                                                              g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                              g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                              g.POR_INCEPTION_DATE != null)
                                                                                                                                                       .ToList());

                    result.ThirdLastYearReturn = preference.EntityNodeType == null ? (benchmarkThirdLastYearRecord != null
                        ? benchmarkThirdLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(result);
        }