通过排序从变量列表创建多级列表

时间:2018-01-01 15:32:26

标签: c# revit

class Dimensions
        {
          public int b { get; set; }
          public int d { get; set; }
          public int CutLength { get; set; }
        }

    Public void FramingID()

{
 var DimList = new List<Dimensions>();
        DimList.Add(new Dimensions { b = 2, d = 4, CutLength=10});
        DimList.Add(new Dimensions { b = 10,d = 5, CutLength=20});
        DimList.Add(new Dimensions { b = 4, d = 6, CutLength=30});
        DimList.Add(new Dimensions { b = 4, d = 2, CutLength=40});
        DimList.Add(new Dimensions { b = 2, d = 2, CutLength=50});
        DimList.Add(new Dimensions { b = 6, d = 4, CutLength=60});
        DimList.Add(new Dimensions { b = 2, d = 2, CutLength=70});
        DimList.Add(new Dimensions { b = 2, d = 5, CutLength=80});
        DimList.Add(new Dimensions { b = 6, d = 2, CutLength=80});
        DimList.Add(new Dimensions { b = 2, d = 2, CutLength=50});

       var Order = from m in DimList orderby m.b, m.d, m.CutLength select m;
   var Order = from m in DimList orderby m.b, m.d, m.CutLength select m;


    foreach (var n in Order)
    {
      Console.WriteLine( n.b.ToString() + " x " + n.d.ToString() + " x " + n.CutLength.ToString());         
     }
}

结果: 2 x 2 x 50
2 x 2 x 50
2 x 2 x 70
2 x 4 x 10
2 x 5 x 80
4 x 2 x 40
4 x 6 x 30
6 x 2 x 80
6 x 4 x 60
10 x 5 x 20

我正在尝试使用上面代码中的相同逻辑创建多级列表,但区别在于列表中的值不是预定义值a。以下代码中的结果与上面的代码相同,但无法预定义值 它们是需要从一组元素中搜索的值,然后被添加到列表中并相应地按升序排列 如何在不使用for循环或foreach循环的情况下将列表中的值作为整数添加,因为两者都不能用于排序,因为值将单独添加 谢谢

 class Dimensions
    {
      public int b { get; set; }
      public int d { get; set; }
      public int CutLength { get; set; }
    }

Public void FramingID()

{
 var doc = Application.ActiveUIDocument.Document;
FilteredElementCollector Collector = new FilteredElementCollector(doc);
ICollection<Element> StructuralFraming = Collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_StructuralFraming).ToList();

List<int> bIntegerList = (from Element element in StructuralFraming select Convert.ToInt32(doc.GetElement(element.GetTypeId()).LookupParameter("b").AsValueString())).ToList();
List<int> dIntegerList = (from Element element in StructuralFraming select Convert.ToInt32(doc.GetElement(element.GetTypeId()).LookupParameter("d").AsValueString())).ToList();
List<int> ClIntegerList = (from Element element in StructuralFraming select Convert.ToInt32(element.LookupParameter("Cut Length").AsValueString())).ToList();

var DimList = new List<Dimensions>();

DimList.Add(new Dimensions { b = bIntegerList, d = dIntegerList, CutLength = ClIntegerList});

var Order = from m in DimList orderby m.b, m.d, m.CutLength select m;

foreach (var n in Order)    
{           
 TaskDialog.Show("TEST", n.b.ToString() + " x " + n.ToString() + " x " + n.ToString());         
       }
}

1 个答案:

答案 0 :(得分:1)

    List<int> bIntegerList = new List<int> { 2, 5, 6, 3, 4 };
    List<int> dIntegerList = new List<int> { 20, 60, 30, 40, 50 };
    List<int> ClIntegerList = new List<int> { 300, 300, 200, 500, 600 };


   var wrapperList = bIntegerList.Zip(dIntegerList, (b, d) => new { b, d });

   var dimListReal = wrapperList.Zip(ClIntegerList, (w, cl) => new Dimensions() { b = w.b, d = w.d, CutLength = cl });

   var Order = from m in dimListReal orderby m.b, m.d, m.CutLength select m;

    foreach (var n in Order)
      {
         Console.WriteLine("Test working " + n.b.ToString() + " x " + n.d.ToString() + " x " + n.CutLength.ToString());
      }

来自revit

    var doc = Application.ActiveUIDocument.Document;
    FilteredElementCollector Collector = new FilteredElementCollector(doc);
    ICollection<Element> StructuralFraming = Collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_StructuralFraming).ToList();      



    List<int> bIntegerList = new List<int> (from Element element in StructuralFraming select Convert.ToInt32(doc.GetElement(element.GetTypeId()).LookupParameter("b").AsValueString())).ToList();
    List<int> dIntegerList = new List<int>(from Element element in StructuralFraming select Convert.ToInt32(doc.GetElement(element.GetTypeId()).LookupParameter("d").AsValueString())).ToList();
    List<int> ClIntegerList = new List<int>(from Element element in StructuralFraming select Convert.ToInt32(element.LookupParameter("Cut Length").AsValueString())).ToList();



    var wrapperList = bIntegerList.Zip(dIntegerList, (b, d) => new { b, d });
    var dimListReal = wrapperList.Zip(ClIntegerList, (w, cl) => new Dimensions() { b = w.b, d = w.d, CutLength = cl });
    var Order = from m in dimListReal orderby m.b, m.d, m.CutLength select m;

        foreach (var n in Order)
        {
           TaskDialog.Show("Test", n.b.ToString() + " x " + n.d.ToString() + " x " + n.CutLength.ToString());
        }

因此,您必须拥有一些 标识符 ,它将3个维度作为同一实体的一部分。我们在ElementX代表 维度 。它可以是bdCutLength。每个ElementX都有一个标识符,用于将其绑定到其他维度值。例如,如果您提交新的 Trio 尺寸,它将如下所示:

ElementX dimensionB = new ElementX { Xvalue = 10 , Id = 999 }
ElementX dimensionD = new ElementX { Xvalue = 80 , Id = 999 }
ElementX dimensionCutLength = new ElementX { Xvalue = 800 , Id = 999 }

还有测试代码

static void Main(string[] args)
    {
        List<ElementX> bIntegerList = new List<ElementX> { new ElementX { Xvalue = 6, Id = 77},
                                                           new ElementX { Xvalue = 3, Id = 66 },
                                                           new ElementX { Xvalue = 8, Id = 65 } };

        List<ElementX> dIntegerList = new List<ElementX> { new ElementX { Xvalue = 30, Id = 66},
                                                           new ElementX { Xvalue = 60, Id = 77 },
                                                           new ElementX { Xvalue = 80, Id = 65 } };

        List<ElementX> ClIntegerList = new List<ElementX> { new ElementX { Xvalue = 800, Id = 65},
                                                           new ElementX { Xvalue = 600, Id = 77 },
                                                           new ElementX { Xvalue = 300, Id = 66 } };


        var wrapperList = bIntegerList.Join(dIntegerList,
                                            x => x.Id,
                                            y => y.Id,
                                            (x, y) => new { b = x.Xvalue, d = y.Xvalue, Id = y.Id }).ToList();

        var dimList = wrapperList.Join(ClIntegerList,
                                         x => x.Id,
                                         cl => cl.Id,
                                         (x, cl) => new Dimensions { b = x.b, d = x.d, CutLength = cl.Xvalue }).ToList();

        var Order = from m in dimList orderby m.b, m.d, m.CutLength select m;

        foreach (var n in Order)
        {
            Console.WriteLine("Test working " + n.b.ToString() + " x " + n.d.ToString() + " x " + n.CutLength.ToString());
        }

输出

enter image description here