本帖最后由 雪山飞狐_lzh 于 2011-1-21 08:50 编辑
简单的流程就是先建一个string-int的键值对字典
然后遍历,每发现一个新的字串就在字典中加入一项
否则就在字典的原有项上加一
下面是Linq的方法
结果:
- [CommandMethod("t2")]
- public static void Test2()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- var resSel = ed.SelectAll(new ResultList { { 0, "text" } });
- if (resSel.Status != PromptStatus.OK)
- return;
- using (var tr = db.TransactionManager.StartTransaction())
- {
- var q =
- resSel.Value.GetObjectIds()
- .Select(id => tr.GetObject(id, OpenMode.ForRead) as DBText)
- .GroupBy(t => t.TextString)
- .Select(txts => new { txts.Key, Count = txts.Count() })
- .OrderBy(t => t.Key);
- foreach (var t in q)
- {
- ed.WriteMessage("\n内容:{0}数量:{1}", t.Key, t.Count);
- }
- }
- }
|