123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962 |
- using SuperMap.Data;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- namespace WWPipeLine.MapBasic
- {
- /// <summary>
- /// DatasetVector查询对象
- /// </summary>
- public class DatasetVectorEx
- {
- private readonly string _layerName;
- public DatasetVectorEx(string lyrName)
- {
- _layerName = lyrName;
- resultFields.AddRange(new string[] { "smid" });
- }
- private DatasetVector m_DatasetVector;
- /// <summary>
- /// DatasetVectorEx中的DatasetVector矢量数据集对象
- /// </summary>
- public DatasetVector DatasetVector { set => m_DatasetVector = value; }
- private List<string> resultFields = new List<string>();
- /// <summary>
- /// 查询后,结果字段集
- /// </summary>
- public List<string> ResultFields { get => resultFields; set => resultFields = value; }
- private List<string> orderBy = new List<string>();
- /// <summary>
- /// 查询时,排序字段集
- /// </summary>
- public List<string> OrderBy { get => orderBy; set => orderBy = value; }
- private List<string> groupBy = new List<string>();
- /// <summary>
- /// 查询时,分组字段集
- /// </summary>
- public List<string> GroupBy { get => groupBy; set => groupBy = value; }
- private bool m_HasGeometry = true;
- /// <summary>
- /// HasGeometry 几何学 几何结构
- /// </summary>
- public bool HasGeometry { get => m_HasGeometry; set => m_HasGeometry = value; }
- /// <summary>
- /// 构造查询参数类,设置查询条件。并返回Recordset数据
- /// <para>使用完毕该Recordset后,必须关闭 recordset.Close(); recordset.Dispose(); </para>
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public Recordset QueryToRecordset(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = resultFields?.ToArray(),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- try
- {
- Recordset recordset = m_DatasetVector.Query(queryParameter);
- return recordset;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
- /// <summary>
- /// 构造查询参数类,设置查询条件。并返回DataTable数据
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public DataTable Query(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = resultFields?.ToArray(),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- return ToDataTable(recordset);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return null;
- }
- public DataTable QueryCaption(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = resultFields?.ToArray(),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- return ToDataTableCaption(recordset);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return null;
- }
- /// <summary>
- /// 构造查询参数类,设置查询条件。并返回查询结果。
- /// <para>判断记录集中是否有记录。true 表示该记录集中无数据,此时,RecordCount 的值应为-1。 </para>
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public bool QueryIsEmpty(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = resultFields?.ToArray(),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- return recordset.IsEmpty;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return false;
- }
- /// <summary>
- /// 根据查询条件,查询一个唯一的SMID值。
- /// <para>如果没有查询到SMID,则返回-1</para>
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public Int32 QueryGetSMID(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- //ResultFields = resultFields?.ToArray(),
- ResultFields = new string[] { "smid" },
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- int result = -1;
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- Int32.TryParse(recordset.GetFieldValue("smid").ToString(), out result);
- return result;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 根据条件返回一行一列的值。只能是一个字符串。
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="resultField"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public string QueryGetValueByOnlyField(string sqlWhere, string resultField, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = new string[] { resultField },
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- string result = string.Empty;
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- if (recordset.RecordCount == 1)
- {
- recordset.MoveFirst();
- result = recordset.GetFieldValue(resultField).ToString();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 将Recordset转为DataTable
- /// </summary>
- /// <param name="recordset"></param>
- /// <returns></returns>
- public DataTable ToDataTable(Recordset recordset)
- {
- DataTable dt = new DataTable(_layerName);
- try
- {
- FieldInfos fieldInfos = recordset.GetFieldInfos();
- // 输出字段名 Output the field name
- Int32 count = fieldInfos.Count;
- for (Int32 i = 0; i < count; i++)
- {
- String fieldname = fieldInfos[i].Name;
- if (resultFields != null && resultFields.Count > 0)
- {
- if (resultFields.Contains(fieldname))
- dt.Columns.Add(fieldname);
- // dt.Columns.Add(fieldInfos[i].Caption);
- }
- else
- {
- dt.Columns.Add(fieldname);
- // dt.Columns.Add(fieldInfos[i].Caption);
- }
- }
- // 输出字段值 Output the field value
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- DataRow dr = dt.NewRow();
- var actIndex = 0;
- for (Int32 j = 0; j < count; j++)
- {
- if (resultFields != null && resultFields.Count > 0)
- {
- if (resultFields.Contains(fieldInfos[j].Name))
- {
- var objValue = recordset.GetFieldValue(j);
- String value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- else
- {
- var objValue = recordset.GetFieldValue(j);
- string value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- dt.Rows.Add(dr);
- recordset.MoveNext();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return dt;
- }
- public DataTable ToDataTableCaption(Recordset recordset)
- {
- DataTable dt = new DataTable(_layerName);
- try
- {
- FieldInfos fieldInfos = recordset.GetFieldInfos();
- // 输出字段名 Output the field name
- Int32 count = fieldInfos.Count;
- for (Int32 i = 0; i < count; i++)
- {
- String fieldname = fieldInfos[i].Name;
- String fieldCaption = fieldInfos[i].Caption;
- if (resultFields != null && resultFields.Count > 0)
- {
- if (resultFields.Contains(fieldname))
- dt.Columns.Add(fieldCaption);
- }
- else
- {
- dt.Columns.Add(fieldCaption);
- }
- }
- // 输出字段值 Output the field value
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- DataRow dr = dt.NewRow();
- int actIndex = 0;
- for (Int32 j = 0; j < count; j++)
- {
- if (resultFields != null && resultFields.Count > 0)
- {
- if (resultFields.Contains(fieldInfos[j].Name))
- {
- var objValue = recordset.GetFieldValue(j);
- String value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- else
- {
- var objValue = recordset.GetFieldValue(j);
- string value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- dt.Rows.Add(dr);
- recordset.MoveNext();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return dt;
- }
- #region 在当前的DatasetVector中,获取指定列名的最大值、最小值
- /// <summary>
- /// 在当前的DatasetVector中,获取指定列名的最大值
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public double GetMaxValue(string fieldName)
- {
- var val = MinMaxValue(fieldName, 1);
- if (val == null) return double.NaN;
- else return double.Parse(val.ToString());
- }
- /// <summary>
- /// 在当前的DatasetVector中,获取指定列名的最小值
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public double GetMinValue(string fieldName)
- {
- var val = MinMaxValue(fieldName, -1);
- if (val == null) return double.NaN;
- else return double.Parse(val.ToString());
- }
- /// <summary>
- /// 在当前的DatasetVector中,获取指定列名的最大值和最小值
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public double[] GetMinMaxValue(string fieldName)
- {
- var val = MinMaxValue(fieldName, 0);
- if (val == null) return new double[] { };
- else return (double[])val;
- }
- /// <summary>
- /// 在当前的DatasetVector中,获取指定列名的最大值、最小值
- /// </summary>
- /// <param name="fieldName">列名</param>
- /// <param name="min">min = 0,获取最小值和最大值;-1时获取最小值,1时获取最大值</param>
- /// <returns></returns>
- private object MinMaxValue(string fieldName, int minMax = 0)
- {
- this.m_HasGeometry = false;
- resultFields.Clear();
- resultFields.Add(fieldName);
- if (minMax == 1)
- orderBy.Add(fieldName + " desc");
- else
- orderBy.Add(fieldName + " asc");
- var dt = this.Query(fieldName + " is not null");
- if (dt.Rows.Count > 0)
- {
- if (minMax != 0)
- return double.Parse(dt.Rows[0][0].ToString());
- else
- {
- return new double[]
- {
- double.Parse(dt.Rows[0][0].ToString()),
- double.Parse(dt.Rows[dt.Rows.Count-1][0].ToString())
- };
- }
- }
- return null;
- }
- #endregion
- /// <summary>
- /// 判断当前DatasetVector中,是否包含指定的列名。包含则返回true
- /// </summary>
- /// <param name="_field"></param>
- /// <returns></returns>
- public bool HasField(string _field)
- {
- bool hasField = false;
- for (int i = 0; i < m_DatasetVector.FieldInfos.Count; i++)
- {
- if (m_DatasetVector.FieldInfos[i].Name == _field)
- {
- hasField = true;
- break;
- }
- }
- return hasField;
- }
- /// <summary>
- /// 二维地图集 按照表名、列名返回该列名的字段值及其加总数量的数据表,并按照filed排序
- /// 里面调用UniqueValOrderSum方法。返回列名的字段值的加总数量
- /// </summary>
- /// <param name="_filed"></param>
- /// <param name="_filedName"></param>
- /// <param name="_sumName"></param>
- /// <returns></returns>
- public DataTable GetTableByFieldOnUniqueValSumOrder(string _filed, string _filedName, string _sumName)
- {
- Dictionary<int, double> result = UniqueValOrderSum(_filedName, _sumName);
- DataTable dt = new DataTable()
- {
- TableName = m_DatasetVector.Name
- }; ;
- dt.Columns.Add(_filed);
- foreach (var key in result.Keys)
- {
- var tmpKey = key.ToString();
- if (key == 0)
- tmpKey = "未知";
- if (!dt.Columns.Contains(tmpKey.ToString()))
- dt.Columns.Add(tmpKey.ToString());
- }
- var rows = dt.NewRow();
- rows[0] = m_DatasetVector.Name;
- dt.Rows.Add(rows);
- var rowIndex = 0;
- var drs = dt.Rows[rowIndex++];
- foreach (var key in result.Keys)
- {
- var tmpKey = key.ToString();
- if (key == 0)
- tmpKey = "未知";
- drs[tmpKey] = result[key];
- }
- return dt;
- }
- /// <summary>
- /// 二维地图集 按照表名、列名返回该列名的字段值及其加总数量的数据表
- /// 里面调用UniqueValOrderSum方法。返回列名的字段值的加总数量
- /// </summary>
- /// <param name="_filed"></param>
- /// <param name="_filedName"></param>
- /// <param name="_sumName"></param>
- /// <returns></returns>
- public DataTable GetTableByFieldOnUniqueValSum(string _filed, string _filedName, string _sumName)
- {
- Dictionary<string, double> result = UniqueValSum(_filedName, _sumName);
- DataTable dt = new DataTable()
- {
- TableName = m_DatasetVector.Name
- }; ;
- dt.Columns.Add(_filed);
- foreach (string key in result.Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key;
- if (!dt.Columns.Contains(tmpKey))
- dt.Columns.Add(tmpKey);
- }
- var rows = dt.NewRow();
- rows[0] = m_DatasetVector.Name;
- dt.Rows.Add(rows);
- var rowIndex = 0;
- var drs = dt.Rows[rowIndex++];
- foreach (string key in result.Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key;
- drs[tmpKey] = result[key].ToString().Split('.')[0];
- }
- return dt;
- }
- public DataTable GetValCountSumByField(string _filedCount, string _filedSum, string[] _rowTitle)
- {
- List<Dictionary<string, int>> results = new List<Dictionary<string, int>>();
- //1 查询该字段的所有值和每个值的数量汇总
- var resultUniqueValCount = UniqueValCount(_filedCount);
- var resultUniqueValSum = UniqueValSumInt(_filedCount, _filedSum);
- results.Add(resultUniqueValCount);
- results.Add(resultUniqueValSum);
- //2 根据查询到的Dictionary开始转换为DataTable
- DataTable dt = new DataTable()
- {
- TableName = m_DatasetVector.Name
- };
- //2.1 DataTable增加标题行,显示所有的字段
- dt.Columns.Add(_rowTitle[0]);
- foreach (var key in results[0].Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
- if (!dt.Columns.Contains(tmpKey))
- dt.Columns.Add(tmpKey);
- }
- //2.2 DataTable增加数据行,显示所有的字段对应的数量汇总
- DataRow dr;
- int rowIndex = 0;
- foreach (var result in results)
- {
- dr = dt.NewRow();
- dt.Rows.Add(dr);
- dt.Rows[rowIndex][0] = _rowTitle[rowIndex + 1];
- foreach (var key in result.Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
- dt.Rows[rowIndex][tmpKey] = result[key];
- }
- rowIndex++;
- }
- return dt;
- }
- public DataTable GetValCountSumByFieldOrder(string _filedCount, string _filedSum, string[] _rowTitle)
- {
- List<Dictionary<string, int>> results = new List<Dictionary<string, int>>();
- //1 查询该字段的所有值和每个值的数量汇总
- Dictionary<int, int> resultValOrderCount = UniqueValOrderCount(_filedCount);
- Dictionary<string, int> resultUniqueValCount = resultValOrderCount.ToDictionary(o => o.Key.ToString(), p => p.Value);
- Dictionary<string, int> resultUniqueValSum = UniqueValSumInt(_filedCount, _filedSum);
- results.Add(resultUniqueValCount);
- results.Add(resultUniqueValSum);
- //2 根据查询到的Dictionary开始转换为DataTable
- DataTable dt = new DataTable() { TableName = m_DatasetVector.Name };
- //2.1 DataTable增加标题行,显示所有的字段
- dt.Columns.Add(_rowTitle[0]);
- foreach (var key in results[0].Keys)
- {
- string a = key.ToString();
- string tmpKey = a == "-1" ? "未知" : a;
- if (!dt.Columns.Contains(tmpKey))
- {
- dt.Columns.Add(tmpKey);
- }
- }
- //2.2 DataTable增加数据行,显示所有的字段对应的数量汇总
- DataRow dr;
- int rowIndex = 0;
- foreach (var result in results)
- {
- dr = dt.NewRow();
- dt.Rows.Add(dr);
- dt.Rows[rowIndex][0] = _rowTitle[rowIndex + 1];
- foreach (var key in result.Keys)
- {
- string a = key.ToString();
- string tmpKey = a == "-1" ? "未知" : a;
- dt.Rows[rowIndex][tmpKey] = result[key];
- }
- rowIndex++;
- }
- return dt;
- }
- /// <summary>
- /// 二维地图集 按照表名、列名返回该列名的字段值及其汇总数量的数据表
- /// 里面调用UniqueValCount方法。返回列名的字段值的汇总数量
- /// </summary>
- /// <param name="_file"></param>
- /// <param name="_fileName"></param>
- /// <returns></returns>
- public DataTable GetTableByFieldOnUniqueValCount(string _filed, string _filedName)
- {
- //1 查询该字段的所有值和每个值的数量汇总
- Dictionary<string, int> result = UniqueValCount(_filedName);
- //2 根据查询到的Dictionary开始转换为DataTable
- DataTable dt = new DataTable()
- {
- TableName = m_DatasetVector.Name
- };
- //2.1 DataTable增加第一行,显示所有的字段
- dt.Columns.Add(_filed);
- foreach (var key in result.Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
- if (!dt.Columns.Contains(tmpKey))
- dt.Columns.Add(tmpKey);
- }
- //2.2 DataTable增加第二行,显示所有的字段对应的数量汇总
- var dr = dt.NewRow();
- dr[0] = m_DatasetVector.Name;
- dt.Rows.Add(dr);
- var rowIndex = 0;
- var drs = dt.Rows[rowIndex++];
- foreach (var key in result.Keys)
- {
- string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
- drs[tmpKey] = result[key];
- }
- return dt;
- }
- /// <summary>
- /// 二维地图集 按照表名、列名返回该列名的字段值及其汇总数量的数据表,并按照列名排序。
- /// 里面调用UniqueValOrderCount方法。返回列名的字段值的汇总数量
- /// </summary>
- /// <param name="_filed"></param>
- /// <param name="_filedName"></param>
- /// <returns></returns>
- public DataTable GetTableByFieldOnUniqueValCountOrder(string _filed, string _filedName)
- {
- Dictionary<int, int> result = UniqueValOrderCount(_filedName);
- DataTable dt = new DataTable()
- {
- TableName = m_DatasetVector.Name
- };
- dt.Columns.Add(_filed);
- foreach (var key in result.Keys)
- {
- var tmpKey = key;
- if (!dt.Columns.Contains(tmpKey.ToString()))
- dt.Columns.Add(tmpKey.ToString());
- }
- var dr = dt.NewRow();
- dr[0] = m_DatasetVector.Name;
- dt.Rows.Add(dr);
- var rowIndex = 0;
- var drs = dt.Rows[rowIndex++];
- foreach (var key in result.Keys)
- {
- string tmpKey = key.ToString();
- drs[tmpKey] = result[key];
- }
- return dt;
- }
- /// <summary>
- /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量COUNT
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public Dictionary<string, int> UniqueValCount(string fieldName)
- {
- Dictionary<string, int> result = new Dictionary<string, int>();
- resultFields.Add(fieldName);
- m_HasGeometry = true;
- Recordset recordset = QueryToRecordset("1=1");
- try
- {
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- var val = recordset.GetString(fieldName);
- if (string.IsNullOrEmpty(val))
- val = "未知";
- if (!result.ContainsKey(val))
- {
- result.Add(val, 1);
- }
- else
- {
- result[val] += 1;
- }
- recordset.MoveNext();
- }
- result = result.OrderBy(o => o.Value).ToDictionary(o => o.Key, p => p.Value);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量COUNT,并按照字段值转为int排序
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public Dictionary<int, int> UniqueValOrderCount(string fieldName)
- {
- Dictionary<int, int> result = new Dictionary<int, int>();
- resultFields.Add(fieldName);
- m_HasGeometry = true;
- Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
- try
- {
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- var val = recordset.GetString(fieldName);
- if (string.IsNullOrEmpty(val))
- val = "-1";
- int orderField = 0;
- int.TryParse(val, out orderField);
- if (!result.ContainsKey(orderField))
- {
- result.Add(orderField, 0);
- }
- result[orderField] += 1;
- recordset.MoveNext();
- }
- result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public Dictionary<string, double> UniqueValSum(string fieldName, string fieldSum)
- {
- Dictionary<string, double> result = new Dictionary<string, double>();
- resultFields.Add(fieldName);
- resultFields.Add(fieldSum);
- m_HasGeometry = true;
- Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
- try
- {
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- var val = recordset.GetString(fieldName);
- if (string.IsNullOrEmpty(val))
- val = "未知";
- if (!result.ContainsKey(val))
- {
- result.Add(val, 0);
- }
- double valDouble = 0.00d;
- double.TryParse(recordset.GetString(fieldSum).ToString(), out valDouble);
- result[val] += valDouble;
- recordset.MoveNext();
- }
- result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM
- /// </summary>
- /// <param name="fieldName"></param>
- /// <param name="fieldSum"></param>
- /// <returns></returns>
- public Dictionary<string, int> UniqueValSumInt(string fieldName, string fieldSum)
- {
- Dictionary<string, int> result = new Dictionary<string, int>();
- resultFields.Add(fieldName);
- resultFields.Add(fieldSum);
- m_HasGeometry = true;
- Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
- try
- {
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- var val = recordset.GetString(fieldName);
- if (string.IsNullOrEmpty(val))
- val = "未知";
- if (!result.ContainsKey(val))
- {
- result.Add(val, 0);
- }
- int valInt = 0;
- int.TryParse(recordset.GetString(fieldSum).Split('.')[0], out valInt);
- result[val] += valInt;
- recordset.MoveNext();
- }
- //result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM,并按照字段值转为int排序
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public Dictionary<int, double> UniqueValOrderSum(string fieldName, string fieldSum)
- {
- Dictionary<int, double> result = new Dictionary<int, double>();
- resultFields.Add(fieldName);
- resultFields.Add(fieldSum);
- m_HasGeometry = true;
- Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
- try
- {
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- double valDouble = 0.00d;
- string val = recordset.GetString(fieldName);
- if (string.IsNullOrEmpty(val))
- val = "未知";
- int orderField = 0;
- int.TryParse(val, out orderField);
- if (!result.ContainsKey(orderField))
- {
- result.Add(orderField, 0);
- }
- double.TryParse(recordset.GetString(fieldSum).ToString(), out valDouble);
- result[orderField] += valDouble;
- recordset.MoveNext();
- }
- result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 判断该字段的数据是否存在NULL值。不存在返回true;存在Nul值返回false。
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public bool NullValHas(string fieldName)
- {
- resultFields.Add(fieldName);
- m_HasGeometry = false;
- var dt = this.Query(fieldName + " is null");
- if (dt.Rows.Count > 0)
- return true;
- return false;
- }
- /// <summary>
- /// 该字段存在的Null值的数量
- /// </summary>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public int NullValCount(string fieldName)
- {
- resultFields.Add(fieldName);
- m_HasGeometry = false;
- var dt = this.Query(fieldName + " is null");
- return dt.Rows.Count;
- }
- }
- }
|