internal static EntityTypeQueries BuildEntityTypeQueries(string entityTypeName, QueryObject queryObject, Dialect dialect, MetadataSrv.Metadata metadataSrv)
            {
                var select       = "it.*";
                var filter       = string.Empty;
                var filterExpand = new List <string>();
                var orderBy      = string.Empty;
                var skip         = string.Empty;
                var top          = string.Empty;

                var entityType = metadataSrv.EntityTypes[entityTypeName];

                var fields = entityType.Properties.ToDictionary((it) => it.Key, (it) => it.Value.FieldName);

                var selectFields    = fields;
                var hasCustomSelect = queryObject.Select != null && queryObject.Select.Length > 0;

                if (hasCustomSelect)
                {
                    var keyNames = entityType.Key;
                    selectFields = selectFields.Where((it) => keyNames.Contains(it.Key) || queryObject.Select.Contains(it.Key)).ToDictionary((it) => it.Key, (it) => it.Value);
                }
                select = GetSelect(selectFields, hasCustomSelect, dialect);

                var tableName       = !string.IsNullOrEmpty(queryObject.CustomQueryTable) ? string.Format("({0})", queryObject.CustomQueryTable) : metadataSrv.EntityTypes[entityTypeName].TableName;
                var entityTypeQuery = new StringBuilder(string.Format("SELECT {0} FROM {1} AS it", "{0}", tableName));

                if (queryObject.FilterExpand != null && queryObject.FilterExpand.Count > 0)
                {
                    var splitExpand = DataUtils.SplitExpand(queryObject.FilterExpand.ToArray(), (el) => el.Expand, (el, it, lastInPath) =>
                    {
                        // 'process' se executa doar la cea din urma proprietate de navigare
                        if (lastInPath)
                        {
                            it.Filter = el.Filter;
                        }
                    });

                    //DataUtils.ForEachNavigationFilter(entityTypeName, splitExpand, (parentEntityTypeName, nav, filterExp) =>
                    //{
                    //    var tableNameLocal = metadataSrv.EntityTypes[entityTypeName].TableName;
                    //    var join = GetJoinCondition(parentEntityTypeName, entityTypeName == parentEntityTypeName, nav, false, metadataSrv);
                    //    tableNameLocal = metadataSrv.EntityTypes[nav.EntityTypeName].TableName;

                    //    entityTypeQuery.Append(string.Format(" INNER JOIN {0} ON {1}", tableNameLocal, join));

                    //    if (!string.IsNullOrEmpty(filterExp))
                    //    {
                    //        var childElementFields = metadataSrv.EntityTypes[nav.EntityTypeName].Properties.ToDictionary((it) => it.Key, (it) => it.Value.FieldName);
                    //        filterExpand.Add(string.Format("( {0} )", ReplaceFieldNames(tableNameLocal, filterExp, childElementFields, dialect)));
                    //    }
                    //}, metadataSrv);

                    foreach (var navigationResult in DataUtils.NavigationFilter(entityTypeName, splitExpand, metadataSrv))
                    {
                        var parentEntityTypeName = navigationResult.EntityTypeName;
                        var nav       = navigationResult.NavigationProperty;
                        var filterExp = navigationResult.Filter;

                        var tableNameLocal = metadataSrv.EntityTypes[entityTypeName].TableName;
                        var join           = GetJoinCondition(parentEntityTypeName, entityTypeName == parentEntityTypeName, nav, false, metadataSrv);
                        tableNameLocal = metadataSrv.EntityTypes[nav.EntityTypeName].TableName;

                        entityTypeQuery.Append(string.Format(" INNER JOIN {0} ON {1}", tableNameLocal, join));

                        if (!string.IsNullOrEmpty(filterExp))
                        {
                            var childElementFields = metadataSrv.EntityTypes[nav.EntityTypeName].Properties.ToDictionary((it) => it.Key, (it) => it.Value.FieldName);
                            filterExpand.Add(string.Format("( {0} )", ReplaceFieldNames(tableNameLocal, filterExp, childElementFields, dialect)));
                        }
                    }
                }

                var arrFilter = new List <string>();

                if (queryObject.Filter != null)
                {
                    arrFilter.Add(string.Format("( {0} )", ReplaceFieldNames("it", queryObject.Filter, fields, dialect)));
                }
                if (filterExpand.Count > 0)
                {
                    arrFilter.AddRange(filterExpand);
                }
                if (arrFilter.Count > 0)
                {
                    filter = " WHERE " + string.Join(" AND ", arrFilter);
                }

                if (queryObject.OrderBy != null && queryObject.OrderBy.Length > 0)
                {
                    orderBy = " ORDER BY " + string.Join(", ", queryObject.OrderBy);
                    orderBy = ReplaceFieldNames("it", orderBy, fields, dialect);
                }

                switch (dialect)
                {
                case Dialect.SQL2012:
                case Dialect.SQL2014:
                    if (queryObject.Top != null)
                    {
                        top = string.Format(" FETCH NEXT {0} ROWS ONLY", queryObject.Top);
                    }

                    if (queryObject.Skip != null)
                    {
                        skip = string.Format(" OFFSET {0} ROWS", queryObject.Skip);
                    }
                    break;

                case Dialect.MYSQL:
                    if (queryObject.Top != null)
                    {
                        top = " LIMIT " + queryObject.Top;
                    }

                    if (queryObject.Skip != null)
                    {
                        skip = " OFFSET " + queryObject.Skip;
                    }
                    break;

                default:
                    break;
                }
                var queryCount = "COUNT(*) AS count";

                queryCount = string.Format(entityTypeQuery.ToString() + filter, queryCount);
                entityTypeQuery.Append(filter + orderBy + top + skip);

                var querySelect = string.Format(entityTypeQuery.ToString(), select);
                var queryRoot   = string.Format(entityTypeQuery.ToString(), "it.*");

                return(new EntityTypeQueries()
                {
                    QuerySelect = querySelect,
                    QueryCount = queryCount,
                    QueryRoot = queryRoot
                });
            }
 /**
  * Query entity collection
  */
 public ResultSerialData GetItems(string entityTypeName, QueryObject queryObject)
 {
     return(this.dataAdapterRead.Fetch(entityTypeName, queryObject));
 }