gpt4 book ai didi

c# - 创建新实例时使用类方法

转载 作者:行者123 更新时间:2023-11-30 22:04:44 27 4
gpt4 key购买 nike

我的类(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();

有办法吗?

附加信息:

我不想只在选择中这样做,这是一个例子。我希望能够在实例化类(class)时即时完成。抱歉造成混淆

最佳答案

不,您不能在 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 之外的所有参数时创建公共(public)构造函数,并在构造函数内进行计算。

关于c# - 创建新实例时使用类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com