0

This code multiplies the volume for month 1 by a growth factor:

forecastData.Where(c => c.Date == months.ElementAt(1)).Sum(c =>
     (c.Volume * c.Growth))

I want to be able to use the growth value from month 6 in this expression also. Something where I can use x as the value from month 6 so the outcome would be:

.Sum(c => (c.Volume * c.Growth) * x.Growth)

Is this possible?

The data class:

public class ForecastedVolumeMonthly
{
    public int VolumeMonthlyId { get; set; }

    // Location
    public int DepartureDepoId { get; set; }
    public virtual Depo DepartureDepo { get; set; }

    // Destination
    public int DestinationDepoId { get; set; }
    public virtual Depo DestinationDepo { get; set; }

    // Nationality
    public int NationalityId { get; set; }
    public virtual Country Nationality { get; set; }

    public long? Volume { get; set; }

    public double? Growth { get; set; }

    public DateTime Date { get; set; }
}
danny
  • 1,969
  • 3
  • 16
  • 26

1 Answers1

1

There is no magic Linq syntax to do this, but it is very simple. If you have a single value, compute it outside the loop (Linq Extension):

(Note that it is even better for performance to move the months.ElementAt(1) outside the loop)

DateTime monthParam = months.ElementAt(1);

double? growth = forecastData.Where(x => x.Date.Month == 6)
                                .Select(x => x.Growth).FirstOrDefault();


double? sum = forecastData.Where(c => c.Date == monthParam)
                            .Sum(c =>(c.Volume * c.Growth) * growth);

Edit After comments:

double? sum1 = (from c in forecastData
                where c.Date == monthParam
                let month6Val = forecastData.FirstOrDefault
                                (x => x.DepartureDepo = c.DepartureDepo 
                                    && x.DestinationDepo = c.DestinationDepo)?.Growth
                select c.Volume * c.Growth * month6Val).Sum();
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • is it possible to do this but instead of multiplying by firstOrDefault growth multiply by corresponding growth? e.g. where the nationality, destination and departure match but are different by month – danny Jul 05 '16 at 09:58
  • @danny Are you trying to group by `nationality, destination and departure` and compute the sum for each group ? – Zein Makki Jul 05 '16 at 10:10
  • yeah I want to perform the calculation grouped and then sum the result of each to get and overall total. so i just need the corresponding month 6 growth for each in the moth 1 query. – danny Jul 05 '16 at 10:14
  • @danny I'm really confused with your requirements. Can you provide sample input and expected results and append it to your question ? – Zein Makki Jul 05 '16 at 10:17
  • Sorry maybe I'm wrong but as far as I can tell the first query .Sum(c => (c.Volume * c.Growth)) will multiply volume by the growth in the same row and then sum the result of all the rows? if so I want to add the growth for month 6 into each of those rows and include this value in the calculation and then sum all rows. There is a different growth factor for each departure and destitution. – danny Jul 05 '16 at 10:24
  • @danny you might want to add `x.Date.Month == 6` inside the let query. – Zein Makki Jul 05 '16 at 11:11