Beispiel #1
0
        public static string GetSelectSql <TModel>(ObjectQuery <TModel> query, IEnumerable <string> selectedProperties, DbCommand command = null,
                                                   IBatchRunner runner = null, IList <string> fields = null)
        {
            if (selectedProperties == null || selectedProperties.Count() < 1)
            {
                throw new ArgumentException("The selected properties must be defined.", "selectedProperties");
            }

            var selector    = new StringBuilder(string.Join(",", selectedProperties)).Insert(0, "new (").Append(")");
            var selectQuery = DynamicQueryable.Select(query, selector.ToString());
            var objectQuery = selectQuery as ObjectQuery;

            if (objectQuery == null)
            {
                throw new ArgumentException("The query must be of type ObjectQuery.", "query");
            }

            string selectSql = objectQuery.ToTraceString();

            if (command != null)
            {
                objectQuery.Parameters.CopyTo(command, runner: runner);
            }

            if (fields != null)
            {
                fields.Clear();
                var maps = new NonPublicMember(objectQuery).GetProperty("QueryState").GetField("_cachedPlan").GetField("CommandDefinition")
                           .GetField("_columnMapGenerators").Idx(0).GetField("_columnMap").GetProperty("Element").GetProperty("Properties").Value as Array;
                foreach (var m in maps)
                {
                    var    mm   = new NonPublicMember(m);
                    int    pos  = (int)mm.GetProperty("ColumnPos").Value;
                    string name = (string)mm.GetProperty("Name").Value;
                    while (pos + 1 > fields.Count)
                    {
                        fields.Add(null);
                    }
                    fields[pos] = name;
                }
            }

            return(selectSql);
        }
        public static IList <string> GetPropertyPositions(IQueryable query, IList <string> fields = null)
        {
            if (fields == null)
            {
                fields = new List <string>();
            }
            fields.Clear();
            var objectQuery = query as ObjectQuery;
            var maps        = new NonPublicMember(objectQuery).GetProperty("QueryState").GetField("_cachedPlan").GetField("CommandDefinition")
                              .GetField("_columnMapGenerators").Idx(0).GetField("_columnMap").GetProperty("Element").GetProperty("Properties").Value as Array;

            foreach (var m in maps)
            {
                var    mm   = new NonPublicMember(m);
                int    pos  = (int)mm.GetProperty("ColumnPos").Value;
                string name = (string)mm.GetProperty("Name").Value;
                while (pos + 1 > fields.Count)
                {
                    fields.Add(null);                            //advance the size of [fields] if the pos index is not covered by it
                }
                fields[pos] = name;
            }
            return(fields);
        }