在创建新实例时使用类方法
本文关键字:类方法 实例 创建 新实例 | 更新日期: 2023-09-27 18:14:49
My class:
public class AvailabilityDataWithoutAging
{
public string BranchPlant { get; set; }
public string Location { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public int PiecesPerPalletMaster { get; set; }
public int NumberOfLots { get; set; }
public int NumberOfPalletsConversion { get; set; }
public int AvailablePrimary { get; set; }
public int TempPrimary { get; set; }
public int BlankPrimary { get; set; }
public int HoldAutomaticPrimary { get; set; }
public int HoldSpecificPrimary { get; set; }
public void CalculatePrimaryFromConversion()
{
NumberOfPalletsConversion = AvailablePrimary/PiecesPerPalletMaster;
}
}
我想在此使用方法CalculatePrimaryFromConversion
:
retValue = _data
.GroupBy(av => new {av.limcu, av.lilocn, av.imlitm})
.Select(av => new AvailabilityDataWithoutAging
{
BranchPlant = av.Key.limcu,
Location = av.Key.lilocn,
ItemCode = av.Key.imlitm,
ItemDescription = av.Max(s => s.imdsc),
PiecesPerPalletMaster = Convert.ToInt32(_JDE8dal.GetF41002Conversion(av.Max(s=>s.liitm),"PL","EA")),
AvailablePrimary = av.Sum(s => s.lipqoh),
NumberOfLots = av.Count(s => s.lilotn.StartsWith("1")),
TempPrimary = av.Sum(s => s.lilotn=="TEMP" ? s.lipqoh : 0),
BlankPrimary = av.Sum(s => s.lilotn == "" ? s.lipqoh : 0),
HoldAutomaticPrimary = 0,
HoldSpecificPrimary = 0
}).ToList();
有办法吗?
附加信息:
我不想只在select中这样做,这是一个例子。我希望能够做到这一点在飞行,而我实例化类。很抱歉造成混乱
不,您不能在Select表达式中这样做。您需要循环遍历结果列表,并对每个项调用CalculaePrimaryFromConversion。
但是我建议你用另一种方法。
设置NumberOfPalletsConversion为计算属性
public class AvailabilityDataWithoutAging
{
public string BranchPlant { get; set; }
public string Location { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public int PiecesPerPalletMaster { get; set; }
public int NumberOfLots { get; set; }
public int NumberOfPalletsConversion
{
get
{
return AvailablePrimary/PiecesPerPalletMaster;
}
}
public int AvailablePrimary { get; set; }
public int TempPrimary { get; set; }
public int BlankPrimary { get; set; }
public int HoldAutomaticPrimary { get; set; }
public int HoldSpecificPrimary { get; set; }
}
另一种方法是在输入除NumberOfPalletsConversion以外的所有参数时创建一个公共构造函数,并在构造函数中进行计算。
.Select(av =>
{
var r = new AvailabilityDataWithoutAging
{
BranchPlant = av.Key.limcu,
Location = av.Key.lilocn,
ItemCode = av.Key.imlitm,
ItemDescription = av.Max(s => s.imdsc),
PiecesPerPalletMaster = Convert.ToInt32(_JDE8dal.GetF41002Conversion(av.Max(s=>s.liitm),"PL","EA")),
AvailablePrimary = av.Sum(s => s.lipqoh),
NumberOfLots = av.Count(s => s.lilotn.StartsWith("1")),
TempPrimary = av.Sum(s => s.lilotn=="TEMP" ? s.lipqoh : 0),
BlankPrimary = av.Sum(s => s.lilotn == "" ? s.lipqoh : 0),
HoldAutomaticPrimary = 0,
HoldSpecificPrimary = 0
};
r.CalculatePrimaryFromConversion();
return r;
})