【问题标题】:need help in grouping the boxes according to their property values需要帮助根据其属性值对框进行分组
【发布时间】:2014-02-24 20:26:28
【问题描述】:

这里创建了一个列表,其中存储了一个盒子的长度、高度和深度。现在,我需要对长度、高度、深度和体积相等的盒子进行分组。我需要创建具有相同长度、高度、深度的盒子的组,除了 bno(盒子编号)。请帮我解决这个问题。 @serv 这是我的实际代码,您可以根据需要进行更改。

    namespace ReadInputfromText
{
    class Box
    {
        private string bno;
        private double length;
        private double height;
        private double depth;
        private double volume;

        // Declare a number of box of type string:
        public string bnumber
        {
            get
            {
                return bno;
            }
            set
            {
                bno = value;
            }
        }

        // Declare  properties of box of type double:
        public double blength
        {
            get
            {
                return length;
            }
            set
            {
                length = value;
            }
        }
        public double bheight
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }
        public double bdepth
        {
            get
            {
                return depth;
            }
            set
            {
                depth = value;
            }
        }
        public double bvolume
        {
            get
            {
                return volume;
            }
            set
            {
                volume = value;
            }
        }

        public static void boxdetails(string[] args)
        {
            String line;
            List<Box> listofboxes = new List<Box>();
            try
            {
                using (StreamReader sr = new StreamReader("c:/containervalues.txt"))

                    while ((line = sr.ReadLine()) != null)
                    {
                        // create new instance of container for each line in file
                        Box box = new Box();
                      //  List<Box> listofboxes = new List<Box>();
                        string[] Parts = line.Split(' ');
                        // set non-static properties of container
                        box.bno = Parts[0];
                        box.length = Convert.ToDouble(Parts[1]);
                        box.height = Convert.ToDouble(Parts[2]);
                        box.depth = Convert.ToDouble(Parts[3]);
                        box.volume = Convert.ToDouble(Parts[4]);
                        // add container to list of containers
                        listofboxes.Add(box);

                    }
                listofboxes = listofboxes.OrderBy(x => x.volume).ToList();
                var groupedBoxes = listofboxes.GroupBy(b => new { b.depth, b.height, b.length }).Dump();
            }

            catch (FileNotFoundException e)
            {
                // FileNotFoundExceptions are handled here.
            }



    }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    试试这个分组:(我在 LinqPad 中一起划过代码)

    void Main()
    {
        var boxes = new List<Box>()
        {
          new Box(1, 2, 2, 2),
          new Box(2, 2, 2, 2),
          new Box(3, 3, 3, 3),
          new Box(4, 3, 3, 3),
          new Box(5, 4, 4, 4)
        };
    
        var groupedBoxes = boxes.GroupBy (b => new {b.depth, b.height, b.length}).Dump();
    }
    
    // Define other methods and classes here
    public class Box
    {
      public Box(int bno, int len, int hei, int dep)
      {
        this.bno = bno; this.length = len; 
        this.height = hei; this.depth = dep; 
        //assuming volume is result of former 3 properties
        this.volume = length * depth * height;
      }
    
    
        public int bno { get; set; }
        public int length { get; set; }
        public int height { get; set; }
        public int depth { get; set; }
        public int volume { get; set; }
    }
    

    输出:(忽略最后一行,总结的地方,只在LinqPad中)

    【讨论】:

    • 您好。你能看看这个吗
    • Manju,你更新的代码看起来不错。只需删除 .Dump(),它在 Visual Studio 中不可用,它应该可以工作。
    • 您可能需要将分组编辑为 blength、bdepth 和 bheigth。因为那是你的公共 Box 类属性。
    【解决方案2】:

    你可以使用 linq 和 groupby,长度、高度和深度相乘会得到体积,所以你只需要按体积分组。

    因此:

    var groupedList = listofboxes.Groupby(item=> item.Volume);
    

    【讨论】:

    • 等体积,不代表边等长(高、深、长)
    猜你喜欢
    • 2011-04-15
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多