Linq对对象进行范围排序

时间:2021-12-22 19:34:44

ObjectIdCollection ids = STools.GetSelection();
            if (ids.Count == 0) return;

            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                List<DBText> sDBTxt = new List<DBText>();
                foreach (ObjectId id in ids)
                {
                    DBText sT = trans.GetObject(id, OpenMode.ForWrite) as DBText;
                    sDBTxt.Add(sT);
                }

        //按x升序排列,按y值降序排列,可以先排序
                var q0 = from e in sDBTxt
                         orderby e.Position.X
                         orderby e.Position.Y descending
                         select e;
        
        //方法2,推荐
                var q1 = sDBTxt.Where(p => p.Position.Y < 658 && p.Position.Y > 150);

                ed.WriteMessage("\n取y坐标值 < 658 且 > 150 的值");
                foreach (DBText st in q1)
                {
                    ed.WriteMessage("\nText:{0,-10}Y:{1,-20}", st.TextString, st.Position.Y);
                }