protected IEnumerable <Row> CreateRows(DbSetInfo dbSetInfo, IEnumerable <object> dataSource, int rowCount) { var fields = dbSetInfo.fieldInfos.Where(f => f.isIncludeInResult()).OrderBy(f => f._ordinal).ToArray(); int fieldsCnt = fields.Length; FieldInfo[] pkInfos = dbSetInfo.GetPKFieldInfos(); Row[] rows = new Row[rowCount]; int counter = 0; foreach (object entity in dataSource) { Row row = new Row(); string[] pk = new string[pkInfos.Length]; row.values = new string[fieldsCnt]; for (int i = 0; i < fieldsCnt; ++i) { string fv = null; FieldInfo fld = fields[i]; fv = this.DataHelper.GetFieldValueAsString(entity, fld.fieldName); int keyIndex = Array.IndexOf(pkInfos, fld); if (keyIndex > -1) { pk[keyIndex] = fv; } row.values[i] = fv; } row.key = string.Join(";", pk); rows[counter] = row; ++counter; } return(rows); }
private void CreateIncludedResult(DbSetInfo dbSetInfo, IEnumerable <object> inputEntities, string propertyName, string[] nextParts, Dictionary <string, IncludedResult> visited) { var metadata = this.EnsureMetadataInitialized(); bool isChildProperty = false; DbSetInfo nextDbSetInfo = null; var assoc = metadata.associations.Values.Where(a => a.parentDbSetName == dbSetInfo.dbSetName && a.parentToChildrenName == propertyName).FirstOrDefault(); if (assoc != null) { isChildProperty = true; nextDbSetInfo = metadata.dbSets[assoc.childDbSetName]; } else { assoc = metadata.associations.Values.Where(a => a.childDbSetName == dbSetInfo.dbSetName && a.childToParentName == propertyName).FirstOrDefault(); if (assoc == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_INCL_NAVIG_INVALID, propertyName + (nextParts.Length > 0?("." + string.Join(".", nextParts)):""))); } nextDbSetInfo = metadata.dbSets[assoc.parentDbSetName]; } if (visited.ContainsKey(nextDbSetInfo.dbSetName + "." + propertyName)) { return; } int rowCount = 0; object propValue; LinkedList <object> resultEntities = new LinkedList <object>(); foreach (object entity in inputEntities) { propValue = this.DataHelper.GetProperty(entity, propertyName); if (isChildProperty && propValue is IEnumerable) { foreach (object childEntity in (IEnumerable)propValue) { resultEntities.AddLast(childEntity); ++rowCount; } } else if (!isChildProperty && propValue != null) { resultEntities.AddLast(propValue); ++rowCount; } } //create temporary result without rows //fills rows at the end of the method IncludedResult current = new IncludedResult { dbSetName = nextDbSetInfo.dbSetName, rows = new Row[0], names = nextDbSetInfo.fieldInfos.Where(f => f.isIncludeInResult()).OrderBy(f => f._ordinal).Select(fi => fi.fieldName) }; visited.Add(nextDbSetInfo.dbSetName + "." + propertyName, current); if (nextParts.Length > 0) { this.CreateIncludedResult(nextDbSetInfo, resultEntities, nextParts[0], nextParts.Skip(1).ToArray(), visited); } //map rows by PK Dictionary <string, Row> rows = new Dictionary <string, Row>(rowCount); var fields = nextDbSetInfo.fieldInfos.Where(f => f.isIncludeInResult()).OrderBy(f => f._ordinal).ToArray(); int fieldCnt = fields.Length; FieldInfo[] pkInfos = nextDbSetInfo.GetPKFieldInfos(); int counter = 0; foreach (object entity in resultEntities) { Row row = new Row(); string[] pk = new string[pkInfos.Length]; row.values = new string[fieldCnt]; for (int i = 0; i < fieldCnt; ++i) { string fv = null; FieldInfo fld = fields[i]; fv = this.DataHelper.GetFieldValueAsString(entity, fld.fieldName); int keyIndex = Array.IndexOf(pkInfos, fld); if (keyIndex > -1) { pk[keyIndex] = fv; } row.values[i] = fv; } row.key = string.Join(";", pk); //here we filter out repeated rows if (!rows.ContainsKey(row.key)) { rows.Add(row.key, row); ++counter; } } current.rows = rows.Values; current.rowCount = counter; }