2

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

I want to use method CalculatePrimaryFromConversion within this:

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();

Is there a way to do it?

ADDITIONAL INFO:

I do not want to do it only within select, this was an example. I want to be able to do it on the fly while i instantiate the class. Sorry for the confusion

1
  • What about creating & using a read-only property, such as NumberOfPalletsConversion? Commented Jul 26, 2014 at 7:33

2 Answers 2

2

No, you can't do that within the Select expression. You need to loop over the resulting list and call CalculaePrimaryFromConversion on each item.

However I would recommend another approach.

Make NumberOfPalletsConversion a calculated property

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

Another alternative is to create a public constructor when you enter all arguments except NumberOfPalletsConversion and do the calculation inside the constructor.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

While this is a viable alternative to OP's method, does it really answer the question? I think OP wants to call the method within the initialization.
@DavidFrye Why not? He asked how to use the method within a Select operation. And this is how to do it.
Added additional info. Sorry for confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.