需要帮助根据属性值对框进行分组

时间:2014-02-24 11:53:33

标签: c#

这里创建了一个列表,用于存储盒子的长度,高度和深度。现在,我需要将具有相同长度,高度,深度和体积的盒子分组。我需要创建一个组,其中包含相同长度,高度,深度的框,除了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.
            }



    }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试这种分组:(我在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中)

enter image description here

答案 1 :(得分:0)

你可以使用linq和groupby,长度,高度和深度相乘会给你音量,所以你只需按体积分组。

因此:

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