我正在向 a 添加一堆不同的零件,List<>
其中一些零件可能具有相同的零件编号和相同的长度。如果它们确实具有相同的零件编号和相同的长度,我需要将这些零件分组以进行显示。
当它们被分组时,我需要显示该零件号以及该零件号中有多少具有特定长度。
我需要知道如何使用两个不同的属性进行分组,并返回一个带有该属性的类型对象List<ICutPart>
以及总数
以下是我所能得到的,我试图返回(IGrouping<int,ICutPart>)sGroup;
,但在函数体的返回部分出现错误。
如何返回一个类型化的对象Group{List<ICutPart> Parts, Int Total}
?
public class CutPart : ICutPart
{
public CutPart() { }
public CutPart(string name, int compID, int partID, string partNum, decimal length)
{
this.Name = name;
this.PartID = partID;
this.PartNumber = partNum;
this.CompID = compID;
this.Length = length;
}
public CutPart(string name, int compID, int partID, string partNum, decimal width, decimal height)
{
this.Name = name;
this.CompID = compID;
this.PartNumber = partNum;
this.PartID = partID;
this.Width = width;
this.Height = height;
this.SF = decimal.Parse(((width / 12) * (height / 12)).ToString(".0000")); //2dp Number;
}
public string Name { get; set; }
public int PartID { get; set; }
public string PartNumber { get; set; }
public int CompID { get; set; }
public decimal Length { get; set; }
public decimal Width { get; set; }
public decimal Height { get; set; }
public decimal SF { get; set; }
}
public class CutParts : List<ICutPart>
{
public IGrouping<int, ICutPart> GroupParts()
{
var sGroup = from cp in this
group cp by cp.Length into g
select new
{
CutParts = g,
total = g.Count()
};
return (IGrouping<int, ICutPart>)sGroup;
}
public new void Add(ICutPart item)
{
base.Add(item);
}
}