Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要根据框的属性值对框进行分组的帮助吗_C# - Fatal编程技术网

C# 需要根据框的属性值对框进行分组的帮助吗

C# 需要根据框的属性值对框进行分组的帮助吗,c#,C#,在这里,创建了一个列表,其中存储长方体的长度、高度和深度。现在,我需要对长度、高度、深度和体积相等的盒子进行分组。我需要创建一个组,其中除了bno(盒子编号)外,其他盒子的长度、高度和深度都相同。请帮我解决这个问题@这是我的实际代码,你可以随时更改 namespace ReadInputfromText { class Box { private string bno; private double length; priv

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

    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.
            }



    }
    }
}
名称空间ReadInputfromText
{
类框
{
私有字符串bno;
私人双倍长度;
私人双高;
私人双深度;
私人双卷;
//声明字符串类型的框的数量:
公共字符串B编号
{
收到
{
返回bno;
}
设置
{
bno=数值;
}
}
//声明类型为double的框的属性:
公共双混合
{
收到
{
返回长度;
}
设置
{
长度=值;
}
}
公共双倍重量
{
收到
{
返回高度;
}
设置
{
高度=数值;
}
}
公共双盲
{
收到
{
返回深度;
}
设置
{
深度=值;
}
}
公共双容积
{
收到
{
返回量;
}
设置
{
体积=价值;
}
}
公共静态void-boxdetails(字符串[]args)
{
弦线;
List LISTOFBOKS=新列表();
尝试
{
使用(StreamReader sr=newstreamreader(“c:/containervalues.txt”))
而((line=sr.ReadLine())!=null)
{
//为文件中的每一行创建容器的新实例
Box=新的Box();
//List LISTOFBOKS=新列表();
字符串[]部分=行分割(“”);
//设置容器的非静态属性
box.bno=零件[0];
box.length=Convert.ToDouble(部分[1]);
box.height=Convert.ToDouble(部分[2]);
box.depth=Convert.ToDouble(第[3]部分);
box.volume=Convert.ToDouble(第[4]部分);
//将容器添加到容器列表中
列表框。添加(框);
}
listofBox=listofBox.OrderBy(x=>x.volume.ToList();
var groupedBoxes=listofBox.GroupBy(b=>new{b.depth,b.height,b.length}).Dump();
}
catch(filenotfounde异常)
{
//FileNotFoundExceptions在这里处理。
}
}
}
}
试试这个分组:(我已经在LinqPad中把代码刮到了一起)

void Main()
{
变量框=新列表()
{
新盒子(1,2,2,2),
新盒子(2,2,2,2),
新盒子(3,3,3,3),
新盒子(4,3,3,3),
新盒子(5,4,4,4)
};
var groupedbox=box.GroupBy(b=>new{b.depth,b.height,b.length}).Dump();
}
//在此处定义其他方法和类
公共类箱
{
公用信箱(内线号码、内线号码、内线号码、内线号码)
{
this.bno=bno;this.length=len;
this.height=hei;this.depth=dep;
//假设体积是前3个属性的结果
这个。体积=长度*深度*高度;
}
公共int bno{get;set;}
公共整数长度{get;set;}
公共整数高度{get;set;}
公共整数深度{get;set;}
公共int卷{get;set;}
}
输出:(忽略最后一行,在那里汇总,仅在LinqPad中)


您可以使用linq和groupby,长度、高度和深度相乘将得到体积,因此您只需按体积分组即可

因此:

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

体积相等,并不意味着边缘长度(高度、深度、长度)相等。你能看看这个manju吗,你的更新代码看起来不错。只需删除Visual Studio中不可用的
.Dump()
,它应该可以工作。您可能需要将分组编辑为blength、bdepth和bheigth。因为这是您的公共框类属性。
var groupedList = listofboxes.Groupby(item=> item.Volume);