public QueryPlan GetQuery(string type, string[] paths, string[] includes = null) { var typeMapping = _metaInfoProvider.GetTypeMapping(type); var tables = typeMapping.Tables.OfType<EntityTable>().ToList(); var mainTable = tables.First(); var mainTableContext = GetTableContext(mainTable, "M"); var selectParts = new List<SelectPart>(); var selectBuilder = new SelectSqlBuilder(mainTableContext).SelectAll(mainTableContext); var mainPart = new TypePart { Type = typeMapping, Tables = new List<Table>(tables) }; selectParts.Add(mainPart); if (includes != null) { foreach (var include in includes) { selectParts.AddRange(Include(selectBuilder, mainTableContext, typeMapping, include)); } } foreach (var table in tables.Skip(1)) { var secondContext = GetTableContext(table, "T"); selectBuilder .SelectAll(secondContext) .InnerJoin(mainTableContext, secondContext, table.IdentityColumn.ColumnName, table.IdentityColumn.ColumnName); } foreach (var s in paths) { BuildSelectPredicate(selectBuilder, mainTableContext, typeMapping, s); } var selectClause = new SelectClause{ Parts = selectParts.ToArray() }; return new QueryPlan { SqlString = selectBuilder.GetSql(), SelectClause = selectClause }; }