/// <summary> /// Queries for the rows that satisfy the attribute query as specified by an <paramref name="filter" /> object /// and executes the specified <paramref name="action" /> on each row returned from the query. /// </summary> /// <param name="source">The source.</param> /// <param name="filter">The attribute requirement that features must satisify.</param> /// <param name="action">The action to take for each feature in the cursor.</param> /// <param name="recycling"> /// if set to <c>true</c> when the cursor rehydrates a single row object on each fetch and can be /// used to optimize read-only access. /// </param> /// <returns> /// Returns a <see cref="int" /> representing the number of rows affected by the action. /// </returns> /// <exception cref="System.ArgumentNullException">action</exception> /// <exception cref="ArgumentNullException">action</exception> /// <remarks> /// Uses a recycling cursors rehydrate a single feature object on each fetch and can be used to optimize read-only /// access /// </remarks> public static int Fetch(this ITable source, IQueryFilter filter, Action <IRow> action, bool recycling) { if (source == null) { return(0); } if (action == null) { throw new ArgumentNullException("action"); } int recordsAffected = 0; using (ComReleaser cr = new ComReleaser()) { ICursor cursor = source.Search(filter, recycling); cr.ManageLifetime(cursor); foreach (var row in cursor.AsEnumerable()) { action(row); recordsAffected++; } } return(recordsAffected); }
/// <summary> /// Reads database rows as a (lazily-evaluated) sequence of objects that are converted into the entity type. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="source">The source.</param> /// <returns> /// Returns a <see cref="IEnumerable{TEntity}" /> representing the entity object. /// </returns> public static IEnumerable <TEntity> Map <TEntity>(this ICursor source) where TEntity : Entity { foreach (var row in source.AsEnumerable()) { yield return(row.ToEntity <TEntity>()); } }
/// <summary> /// Gets the rows that reside in the version that corresponds to the state. /// </summary> /// <param name="changeVersion">The change version.</param> /// <returns> /// Returns a <see cref="IEnumerable{IRow}" /> representing the rows for the state. /// </returns> /// <exception cref="System.NotSupportedException">The row state is not supported.</exception> public IEnumerable <IRow> GetRows(DeltaRowChangeVersion changeVersion) { IFeatureWorkspace workspace = this.GetWorkspace(changeVersion); if (workspace != null) { ITable table = workspace.OpenTable(this.ModifiedClassInfo.ChildClassName); ICursor cursor = null; try { foreach (var oids in this.Select(o => o.OID.ToString(CultureInfo.InvariantCulture)).Batch(1000)) { IQueryFilter filter = new QueryFilterClass(); filter.WhereClause = string.Format("{0} IN ({1})", table.OIDFieldName, string.Join(",", oids.ToArray())); cursor = table.Search(filter, false); foreach (var row in cursor.AsEnumerable()) { yield return(row); } } } finally { if (cursor != null) { while (Marshal.ReleaseComObject(cursor) > 0) { } } } } }
/// <summary> /// Copies the rows from the <paramref name="data" /> table to the source table. /// </summary> /// <param name="source">The source.</param> /// <param name="data">The data that will be copied.</param> /// <param name="filter">The filter that will be used prior to copying the data.</param> /// <param name="progress">The progress callback.</param> /// <returns> /// Returns a <see cref="List{T}" /> representing the object ids of the records loaded. /// </returns> /// <remarks> /// Assumes that the source and data table have the same schema. /// </remarks> public static List <int> Load(this ITable source, ITable data, IQueryFilter filter = null, IFeatureProgress progress = null) { if (progress != null) { progress.FeatureClassName = ((IDataset)source).Name; progress.MinFeatures = 0; progress.MaxFeatures = data.RowCount(filter); progress.Position = 0; progress.StepValue = 1; } var oids = new List <int>(); var fields = source.Fields.ToDictionary(f => f.Editable); using (ComReleaser cr = new ComReleaser()) { ICursor cursor = data.Search(filter, false); cr.ManageLifetime(cursor); ICursor insert = source.Insert(true); cr.ManageLifetime(insert); var buffer = source.CreateRowBuffer(); foreach (var rows in cursor.AsEnumerable().Batch(1000)) { foreach (var row in rows) { foreach (var field in fields) { buffer.Update(field.Value, row.Value[field.Value], false); } var oid = (int)insert.InsertRow(buffer); oids.Add(oid); if (progress != null) { progress.Step(); } } insert.Flush(); } insert.Flush(); } return(oids); }
/// <summary> /// Queries for the rows that satisfy the attribute query as specified by an <paramref name="filter" /> object. /// </summary> /// <param name="source">The source.</param> /// <param name="filter">The attribute requirement that the rows must satisify.</param> /// <returns> /// Returns a <see cref="List{IRow}" /> representing the rows returned from the query. /// </returns> public static IList <IRow> Fetch(this ITable source, IQueryFilter filter) { if (source == null) { return(null); } using (ComReleaser cr = new ComReleaser()) { ICursor cursor = source.Search(filter, false); cr.ManageLifetime(cursor); return(cursor.AsEnumerable().ToList()); } }
/// <summary> /// Converts the contents of the cursor into an XML document. /// </summary> /// <param name="source">The cursor.</param> /// <param name="elementName">Name of the element.</param> /// <param name="predicate">The predicate to determine if the field should be included.</param> /// <returns> /// Returns a <see cref="XDocument" /> representing the contents of the cursor. /// </returns> public static XDocument GetXDocument(this ICursor source, string elementName, Predicate <IField> predicate) { if (source == null) { return(null); } if (elementName == null) { throw new ArgumentNullException("elementName"); } if (predicate == null) { throw new ArgumentNullException("predicate"); } XElement table = new XElement(elementName, new XAttribute("Timestamp", DateTime.Now.ToString("f"))); XDocument doc = new XDocument(table); // Iterate through all of the records. foreach (var row in source.AsEnumerable()) { // An element that represents a row. XElement element = new XElement("Row"); // Iterate through all of the fields and output their values. for (int i = 0; i < row.Fields.FieldCount; i++) { IField field = row.Fields.Field[i]; if (predicate(field)) { object o = row.Value[i]; object value = (DBNull.Value == o || o == null) ? "" : o.ToString(); element.Add(new XElement("Field", new XAttribute("Name", field.Name), new XAttribute("Value", value))); } } // Add the rows to the table. table.Add(element); } return(doc); }
/// <summary> /// Queries for the features that satisfies the attribute and/or spatial query as specified by an /// <paramref name="filter" /> object and projects the results into a new form. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="source">The source.</param> /// <param name="filter">The attribute and/or spatial requirement that the features must satisify.</param> /// <param name="selector">Projects each element of a sequence into a new form.</param> /// <returns> /// Returns a <see cref="List{TResult}" /> representing the results of the query projected to the type. /// </returns> /// <exception cref="System.ArgumentNullException">selector</exception> public static IList <TResult> Fetch <TResult>(this ITable source, IQueryFilter filter, Func <IRow, TResult> selector) { if (source == null) { return(null); } if (selector == null) { throw new ArgumentNullException("selector"); } using (ComReleaser cr = new ComReleaser()) { ICursor cursor = source.Search(filter, false); cr.ManageLifetime(cursor); return(cursor.AsEnumerable().Select(selector).ToList()); } }
/// <summary> /// Queries for the rows that have the specified object ids. /// </summary> /// <param name="source">The source.</param> /// <param name="oids">The list of object ids.</param> /// <returns> /// Returns a <see cref="List{IRow}" /> representing the rows returned from the query. /// </returns> /// <exception cref="System.ArgumentNullException">oids</exception> public static IList <IRow> Fetch(this ITable source, params int[] oids) { if (source == null) { return(null); } if (oids == null) { throw new ArgumentNullException("oids"); } using (ComReleaser cr = new ComReleaser()) { ICursor cursor = source.GetRows(oids, false); cr.ManageLifetime(cursor); return(cursor.AsEnumerable().ToList()); } }