创建新实例时使用类方法

时间:2014-07-26 07:29:22

标签: c# oop

我的课程:

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中做这个,这是一个例子。我希望能够在实例化课程时动态地完成它。对不起困惑

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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