I have a list of ForecastData
and it looks like this:
public class ForecastData
{
public string SalesID {get;set;}
public string Customer {get;set;}
public string Vendor {get;set;}
public string Division {get;set;}
public int Year {get;set;}
public decimal Amount {get;set;}
}
I need to display a list of every distinct "SalesID" for each "Customer" that has an Amount > 0 where the year is THIS YEAR.
Currently, I'm grouping by Customer, but because I can have multiple "Amounts" for the same Customer and SalesID in my dataset, I'm not getting the results I expect. My results show:
- Customer1 User1 $100
- Customer1 User1 $200
- Customer1 User1 $300
- Customer1 User2 $100
- Customer1 User2 $200
But what I want is
- Customer1 User1 $600
- Customer1 User2 $300
Here's my expression:
var forecasts = (List<ForecastData>)cache.Get(_RAW_FORECAST_DATA_KEY, null);
foreach(var custGroup in forecasts.Where(f => f.Year == DateTime.Now.Year).GroupBy(f => f.Customer))
{
if(custGroup.Count() > 1) // There's more than one forecast for this customer
{
foreach(var instance in custGroup)
{
toReturn.Add(new MultipleCustomer(custGroup.Key)
{
Salesperson = instance.SalesPersonId,
Forecast = instance.Amount
});
}
}
}
return toReturn;