Ejemplo n.º 1
0
        public virtual MdQueryResult Execute(MdQuery q)
        {
            var data = q.SourceData as IEnumerable;
            if (data != null)
            {
                var result = CreateEmptyResult(q);

                var groupSb = new StringBuilder();
                var measureSb = new StringBuilder();

                foreach (var dim in q.Dimensions)
                {
                    var dimModel = this.model.Dimensions.Single(p => p.Name == dim);
                    if (string.IsNullOrEmpty(dimModel.Expression))
                    {
                        groupSb.AppendFormat("{0} as {0},", dim);
                        measureSb.AppendFormat("Key.{0} as {0},", dim);
                    }
                    else
                    {
                        groupSb.AppendFormat("{0} as {1},", dimModel.Expression, dim);
                        measureSb.AppendFormat("Key.{0} as {0},", dim);
                    }
                }
                groupSb.Remove(groupSb.Length - 1, 1);
                var groupQueryString = string.Format("new ({0})", groupSb.ToString());
                var groupQuery = data.AsQueryable().GroupBy(groupQueryString, "it");

                foreach (var measure in q.Measures)
                {
                    var measureData = model.Measures.Single(p => p.Name == measure);
                    if (string.IsNullOrEmpty(measureData.Expression))
                    {
                        measureSb.AppendFormat("{0}({1}) as {1},",
                            AggregateFunc(measureData.Aggregation), measure);
                    }
                    else
                    {
                        measureSb.AppendFormat("{0}({1}) as {2},",
                            AggregateFunc(measureData.Aggregation), measureData.Expression, measure);
                    }
                }
                measureSb.Remove(measureSb.Length - 1, 1);
                var measureQueryString = string.Format("new ({0})", measureSb.ToString());
                var measureQuery = groupQuery.Select(measureQueryString);
                var sortBuffer = new StringBuilder();

                for (int i = 0; i < q.Sort.Count; i++)
                {
                    var sort = q.Sort[i];
                    sortBuffer.AppendFormat("{0} {1},", sort.Column, sort.Direction.ToString());
                }

                if (sortBuffer.Length > 0) {
                    sortBuffer.Remove(sortBuffer.Length-1, 1);
                    var sortExp = sortBuffer.ToString();
                    if (!string.IsNullOrEmpty(sortExp))
                        measureQuery = measureQuery.OrderBy(sortExp);
                }

                if (q.Skip > 0)
                    measureQuery = measureQuery.Skip(q.Skip);

                if (q.Take > 0)
                    measureQuery = measureQuery.Take(q.Take);

                foreach (var resultItem in measureQuery)
                {
                    var row = new List<DataCell>();
                    foreach (var dim in q.Dimensions)
                    {
                        var value = getDynamicValue(resultItem, dim);
                        var formatInfo = model.Dimensions.Single(p => p.Name == dim).DefaultFormat;
                        var cell = new DataCell(value, formatInfo == null ? null: formatInfo.FormatString);
                        row.Add(cell);
                    }

                    foreach (var measure in q.Measures)
                    {
                        var value = getDynamicValue(resultItem, measure);
                        var formatInfo = model.Measures.Single(p => p.Name == measure).DefaultFormat;
                        var cell = new DataCell(value, formatInfo == null ? null : formatInfo.FormatString);
                        row.Add(cell);
                    }
                    result.Items.Add(row);
                }

                return result;
            }
            else return null;
        }
Ejemplo n.º 2
0
        public virtual MdQueryResult Execute(MdQuery q)
        {
            var data = q.SourceData as IEnumerable;

            if (data != null)
            {
                var result = CreateEmptyResult(q);

                var groupSb   = new StringBuilder();
                var measureSb = new StringBuilder();

                foreach (var dim in q.Dimensions)
                {
                    var dimModel = this.model.Dimensions.Single(p => p.Name == dim);
                    if (string.IsNullOrEmpty(dimModel.Expression))
                    {
                        groupSb.AppendFormat("{0} as {0},", dim);
                        measureSb.AppendFormat("Key.{0} as {0},", dim);
                    }
                    else
                    {
                        groupSb.AppendFormat("{0} as {1},", dimModel.Expression, dim);
                        measureSb.AppendFormat("Key.{0} as {0},", dim);
                    }
                }
                groupSb.Remove(groupSb.Length - 1, 1);
                var groupQueryString = string.Format("new ({0})", groupSb.ToString());
                var groupQuery       = data.AsQueryable().GroupBy(groupQueryString, "it");


                foreach (var measure in q.Measures)
                {
                    var measureData = model.Measures.Single(p => p.Name == measure);
                    if (string.IsNullOrEmpty(measureData.Expression))
                    {
                        measureSb.AppendFormat("{0}({1}) as {1},",
                                               AggregateFunc(measureData.Aggregation), measure);
                    }
                    else
                    {
                        measureSb.AppendFormat("{0}({1}) as {2},",
                                               AggregateFunc(measureData.Aggregation), measureData.Expression, measure);
                    }
                }
                measureSb.Remove(measureSb.Length - 1, 1);
                var measureQueryString = string.Format("new ({0})", measureSb.ToString());
                var measureQuery       = groupQuery.Select(measureQueryString);
                var sortBuffer         = new StringBuilder();

                for (int i = 0; i < q.Sort.Count; i++)
                {
                    var sort = q.Sort[i];
                    sortBuffer.AppendFormat("{0} {1},", sort.Column, sort.Direction.ToString());
                }

                if (sortBuffer.Length > 0)
                {
                    sortBuffer.Remove(sortBuffer.Length - 1, 1);
                    var sortExp = sortBuffer.ToString();
                    if (!string.IsNullOrEmpty(sortExp))
                    {
                        measureQuery = measureQuery.OrderBy(sortExp);
                    }
                }

                if (q.Skip > 0)
                {
                    measureQuery = measureQuery.Skip(q.Skip);
                }

                if (q.Take > 0)
                {
                    measureQuery = measureQuery.Take(q.Take);
                }

                foreach (var resultItem in measureQuery)
                {
                    var row = new List <DataCell>();
                    foreach (var dim in q.Dimensions)
                    {
                        var value      = getDynamicValue(resultItem, dim);
                        var formatInfo = model.Dimensions.Single(p => p.Name == dim).DefaultFormat;
                        var cell       = new DataCell(value, formatInfo == null ? null: formatInfo.FormatString);
                        row.Add(cell);
                    }

                    foreach (var measure in q.Measures)
                    {
                        var value      = getDynamicValue(resultItem, measure);
                        var formatInfo = model.Measures.Single(p => p.Name == measure).DefaultFormat;
                        var cell       = new DataCell(value, formatInfo == null ? null : formatInfo.FormatString);
                        row.Add(cell);
                    }
                    result.Items.Add(row);
                }

                return(result);
            }
            else
            {
                return(null);
            }
        }