I want to specify when a cost is first incurred and a repeat period in years that means the cost is incurred again and again. So I have created a Cost model that looks like this:
public class Cost
{
public Cost()
{
Year = 1;
}
public decimal Amount { get; set; }
public int AnsweredQuestionID { get; set;}
public virtual AnsweredQuestion AnsweredQuestion {get; set;}
public int? RepeatPeriod { get; set; }
}
Now I want to return the costs incurred between two dates, preferably using Linq.
Edit I oversimplified my question. I have PropertyCosts that are incurred on a specific Date and again after a period of time. The date the cost is first incurred is calculated from the date the property was surveyed. The costs model stores the RepeatPeriod and is related to a specific Question/Answer. The cost is incurred if a question has been answered in a specific way for a property. So I've got code that looks a bit like this (still trying to simplify here) but at the moment I'm only getting the first occurences
public IEnumerable<PropertyCost> GetCosts(DateTime startDate, DateTime endDate)
{
IQueryable<AnsweredQuestion> answeredQuestions =
_Uow.AnsweredQuestionRepository
.All
.Where(x => x.PropertySurvey.PropertyID == id);
IQueryable<Cost> allCosts = UOW.CostRepository.All;
IQueryable<PropertyCost> firstOccurences = from aq in answeredQuestions
from c in costs
where aq.ID == c.AnsweredQuestionID
select new PropertyCost
{
QuestionText = aq.Question.Text,
AnswerText = aq.Answer.Text,
UnitCost = c.Amount,
Date = aq.PropertySurvey.Survey.StartDate,
RepeatYears = c.RepeatPeriod
});
//but now I need to insert PropertyCosts for recurring costs that occur when RepeatPeriod is not null
}
How about this....
var costs = firstOccurences.SelectMany (p =>
from x in Enumerable.Range(0, (p.RepeatYears ?? 0) > 0
? ((endDate.Year - p.Date.Year)+1)/p.RepeatYears.Value
: 1)
let date = p.Date.AddYears(x * p.RepeatYears??0)
where startDate <= date && endDate >= date
select new PropertyCost {
QuestionText=p.QuestionText,
AnswerText=p.AnswerText,
UnitCost = p.UnitCost,
Date = date,
RepeatYears = p.RepeatYears
}
)
You may need to convert firstOccurences to an Enumerable first due to translations functions
eg
IEnumerable<PropertyCost> firstOccurences = (from aq in answeredQuestions
from c in costs
where aq.ID == c.AnsweredQuestionID
select new PropertyCost
{
QuestionText = aq.Question.Text,
AnswerText = aq.Answer.Text,
UnitCost = c.Amount,
Date = aq.PropertySurvey.Survey.StartDate,
RepeatYears = c.RepeatPeriod
}).AsEnumerable();
It's a quick fix and the initial 0 from the Enumerable.Range could be replaced with a calculation.
First of all, I have no idea why would you like to return collection of Cost items. You should create another class to handle that data, e.g.:
public class CostOccurrence
{
public decimal Amount { get; set; }
public DateTime Occurrence { get; set; }
}
And then implement your method:
public IEnumerable<CostOccurrence> GetCosts(DateTime startDate, DateTime endDate)
{
DateTime current = FirstIncurred;
while (current < startDate)
current = current.AddYears(RepeatPeriod);
while (current >= startDate && current < endDate)
{
yield return new CostOccurrence { Amount = Amount, Occurrence = current };
current = current.AddYears(RepeatPeriod);
}
}
Related
I have written following LINQ query to return list and then iterating list separately to convert time into hours and taking sum of hours for each list item.
I am sure this might not be the right away as it send three database calls.
Can i re-write it in a better way e.g. GroupBy or by some other way to assign data to model, converting IdleTime into hours and than taking sum of IdleTime??
Model Class
public class TestModel
{
public string TaskSummary { get; set; }
public string LocationName { get; set; }
public float IdleTime { get; set; }
public string Description { get; set; }
public float IdleTimeSum { get; set; }
public Guid TaskId { get; set; }
}
LINQ Query
List<TestModel> list = _context.Time
.Include(x => x.Report)
.Include(x => x.Report.Task)
.Include(x => x.Report.Task.Location)
.Where(x => taskIds.Contains(x.Report.TaskId))
.Select(x => new TestModel
{
TaskSummary = x.Report.Task.Summary,
LocationName = x.Report.Task.Location.Name,
IdleTime = x.Duration,
Description = x.Description,
TaskId = x.Report.TaskId,
}).ToList();
How i am converting into hours?
foreach (var item in list)
item.IdleTime = item. IdleTime / 60;
How I am taking sum?
foreach (var item in list)
item.IdleTimeSum = item. IdleTime;
Just add those to your projection:
.Select(x => new TestModel
{
TaskSummary = x.Report.Task.Summary,
LocationName = x.Report.Task.Location.Name,
IdleTime = x.Duration / 60.0f,
Description = x.Description,
TaskId = x.Report.TaskId,
NPTHoursSum = x.Duration / 60.0f,
}).ToList();
Although since you're not showing where you actually sum anything I suspect there's more to the problem than that.
Yeah Select is a projection function, you can just do those operations in your select.
.Select(x => new TestModel
{
TaskSummary = x.Report.Task.Summary,
LocationName = x.Report.Task.Location.Name,
IdleTime = x.Duration /60.0f,
NPTHoursSum = x.Duration / 60.0f,
Description = x.Description,
TaskId = x.Report.TaskId,
}).ToList();
If you're using LINQ to SQL then the division operation will probably occur at the db. If you actually want to do a sum it would have to be in a different query or as a sub query or something.\
I have the below Linq query:
var qry = from Output in db.Outputs
join ShiftHours in db.ShiftHourses on Output.ShiftHour equals ShiftHours.ShiftHour
join ShiftData in db.ShiftDatas on Output.ShiftID equals ShiftData.ShiftID
where ShiftData.ShiftDate == date && ShiftData.Line == line
select new ProgressData()
{
CPM = ShiftData.CPM,
Target = ShiftData.Target,
CurrentOutput = db.Outputs.Sum(x=>x.Quantity),
PercentOfTarget = (db.Outputs.Sum(x=>x.Quantity) / ShiftData.Target) * 100
};
It is almost doing what I want but as it stands, the CurrentOutput lambda expression is returning the sum of the entire Quantity column of the Output table as I am unsure how to add in a 'Where' clause as well as the sum function (and hence the PercentOfTarget is also incorrect).
The where clause needs to be the same as the first where clause (date and line are parameters passed to the method):
where ShiftData.ShiftDate == date && ShiftData.Line == line
Can anyone help?
EDIT: Clarification of CurrentOutput.
In the 'Output' table there can be multiple records for a given 'ShiftData.ShiftDate' and 'ShiftData.Line' combination so I would like to calculate a sum of the 'Output' table 'Quantity' column values for a specified 'ShiftDate' and 'Line'
EDIT: Further clarification
This is some sample data from the Output table (OutputID is an auto-increment PK):
public class Output
{
[Key]
public int OutputId { get; set; }
public int ShiftID { get; set; }
public int Quantity { get; set; }
public int ShiftHour { get; set; }
public virtual ShiftData ShiftData { get; set; }
}
This is some sample data from the ShiftData table (ShiftID is an auto-increment PK, there will be more than one record for each date as further line numbers are added):
public class ShiftData
{
[Key]
public int ShiftID { get; set; }
public DateTime ShiftDate { get; set; }
public string Line { get; set; }
public int CPM { get; set; }
public double Target { get; set; }
}
So using the above sample data, I am trying to populate a ProgressData object:
public class ProgressData
{
public int CPM { get; set; }
public double Target { get; set; }
public int CurrentOutput { get; set; }
public double PercentOfTarget { get; set; }
}
Based on the sample data, I would expect my ProgressData object created for line 1 on 13/2/2014 to be populated as such:
CPM = 5, Target = 200, CurrentOutput = 120, PercentOfTarget = 60
You can try to do group join for that purpose :
var qry = from ShiftData in db.ShiftDatas
join Output in db.Outputs on ShiftData.ShiftID equals Output.ShiftID
into ShiftGroup
where ShiftData.ShiftDate == date && ShiftData.Line == line
select new ProgressData()
{
CPM = ShiftData.CPM,
Target = ShiftData.Target,
CurrentOutput = ShiftGroup.Sum(x=>x.Quantity),
PercentOfTarget = (ShiftGroup.Sum(x=>x.Quantity) / ShiftData.Target) * 100
};
Another thing, I can't see why you need to do join with ShiftHours here, since none of it's property used in select statement.
Just as #har07 posted I managed to get it working using the below. I am posting this for reference as it does answer the original question but I'm going to try and use #har07's code as it's tidier than mine.
var qry = (from Output in db.Outputs
join ShiftHours in db.ShiftHourses on Output.ShiftHour equals ShiftHours.ShiftHour
join ShiftData in db.ShiftDatas on Output.ShiftID equals ShiftData.ShiftID
where ShiftData.ShiftDate == date && ShiftData.Line == line
select new
{
ShiftData.ShiftDate,
ShiftData.Line,
ShiftData.CPM,
ShiftData.Target,
Output.Quantity
}).ToList();
var progress = qry.GroupBy(l => l.ShiftDate).Select(g => new ProgressData()
{
CPM = g.Where(c => c.ShiftDate == date && c.Line == line).Select(c => c.CPM).FirstOrDefault(),
Target = g.Where(c => c.ShiftDate == date && c.Line == line).Select(c => c.Target).FirstOrDefault(),
CurrentOutput = g.Where(c => c.ShiftDate == date && c.Line == line).Sum(c => c.Quantity),
PercentOfTarget = g.Where(c => c.ShiftDate == date && c.Line == line).Sum(c => (c.Quantity / c.Target) * 100)
});
return progress.FirstOrDefault();
I have following code in custom validation attribute called DateRange:
private DateTime _minDate = DateTime.Today.AddYears(-100);
private DateTime _maxDate = DateTime.MaxValue;
// String representation of the Min Date (yyyy/MM/dd)
public string Min
{
get { return FormatDate(_minDate, DateTime.Today.AddYears(-100)); }
set { _minDate = value == "Today" ? DateTime.Today : ParseDate(value, DateTime.Today.AddYears(-100)); }
}
// String representation of the Max Date (yyyy/MM/dd)
public string Max
{
get { return FormatDate(_maxDate, DateTime.MaxValue); }
set { _maxDate = value == "Today" ? DateTime.Today : ParseDate(value, DateTime.MaxValue); }
}
Then I write this attribute in metadata on some property of entity model like this:
[DateRange(Max = "Today")]
public string SomeDateProperty { get; set; };
I set breakpoint on Max property's getter. First time I open view, breakpoint is activated and DateTime.Today is got. Consequent refresh of the view does not activate breakpoint and old value is got. I think it's caching validation attribute. My question is: Is this because of caching? If it is, then how to disable it? Thanks in advance
The constructor for the custom attributes only get hit once, no idea how to turn off any sort of caching. The way I got round this for my scenario, was to only deal with the date calculation in the "IsValid" Method.
I created a date in the past attribute, that needed the date to be in the past, but you could set how long in the past was valid.
public class DateInPastAttribute : ValidationAttribute
{
private const string DefaultErrorMessage = "'{0}' must be in the past.";
public int DaysInPastAllowed { get; set; }
public DateInPastAttribute(int daysInPastAllowed)
: base(DefaultErrorMessage)
{
this.DaysInPastAllowed = daysInPastAllowed;
}
public override bool IsValid(object value)
{
if (!(value is DateTime))
{
return true;
}
DateTime maxDate = DateTime.Now;
DateTime minDate = maxDate.AddDays(this.DaysInPastAllowed * -1);
DateTime dateValue = (DateTime)value;
return
minDate <= dateValue &&
dateValue <= maxDate;
}
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, this.ErrorMessageString, name);
}
}
You can then use it in your view model like this:
[DateInPast(365)]
public DateTime DateReceived { get; set; }
Which would allow a date to be entered within the last year. You could amend this for the scenario that you require.
I have a LINQ query with more than 2 where conditions, but it doesn't seem to evaluate with more than 2 conditions. Is there a way to add more conditions to the where clause?
var query =
from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
where (string)f.Element("departurelocation") == From &&
(string)f.Element("destinationlocation") == DestCity &&
(string)f.Element("airline") == Airline
// && (string)f.Element("departuredate") == DepartDate &&
// (string)f.Element("departuretime")==DepartTime
//&& (string)f.Element("returndate")==ReturnDate &&
//(string)f.Element("returntime")==ReturnTime
orderby Convert.ToInt32(f.Element("price").Value)
select new
{
FlightNumber = (Int32)f.Element("flightnumber"),
Airline = (string)f.Element("airline"),
Departure = (string)f.Element("departureairportsymbol"),
DepartTime = (string)f.Element("departuretime"),
Destination = (string)f.Element("destinationairportsymbol"),
ArrivalTime = (string)f.Element("arrivaltime"),
Stops = (int)f.Element("numberofstops"),
Duration = (string)f.Element("duration"),
Cabin = (string)f.Element("cabin"),
Price = "$" + (Int32)f.Element("price"),
ImagePath = (string)f.Element("airlineimageurl").Value
};
LINQ absolutely allows more than two WHERE conditions. Have you tried separating the query into more manageable pieces? LINQ uses deferred execution anyway so you won't see a performance penalty in doing so.
You should also consider making a class to hold the information you're stuffing into the result.
public class FlightDetail
{
public Int32 FlightNumber { get; set; }
public String Airline { get; set; }
public String Departure { get; set; }
public String DepartureTime { get; set; }
public String Destination { get; set; }
public String ArrivalTime { get; set; }
public Int32 Stops { get; set; }
public String Duration { get; set; }
public String Cabin { get; set; }
public Int32 Price { get; set; }
public String ImagePath { get; set; }
}
Then something like this which is more readable but should also help you find whatever bug is popping up.
var flights =
from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
select new FlightDetail
{
FlightNumber = (Int32)f.Element("flightnumber"),
Airline = (string)f.Element("airline"),
Departure = (string)f.Element("departureairportsymbol"),
DepartTime = (string)f.Element("departuretime"),
Destination = (string)f.Element("destinationairportsymbol"),
ArrivalTime = (string)f.Element("arrivaltime"),
Stops = (int)f.Element("numberofstops"),
Duration = (string)f.Element("duration"),
Cabin = (string)f.Element("cabin"),
Price = "$" + (Int32)f.Element("price"),
ImagePath = (string)f.Element("airlineimageurl").Value
};
var flightsByLocation =
flights.
where (string)f.Element("departurelocation") == From &&
(string)f.Element("destinationlocation") == DestCity
select new FlightDetail
{
FlightNumber = (Int32)f.Element("flightnumber"),
Airline = (string)f.Element("airline"),
Departure = (string)f.Element("departureairportsymbol"),
DepartTime = (string)f.Element("departuretime"),
Destination = (string)f.Element("destinationairportsymbol"),
ArrivalTime = (string)f.Element("arrivaltime"),
Stops = (int)f.Element("numberofstops"),
Duration = (string)f.Element("duration"),
Cabin = (string)f.Element("cabin"),
Price = "$" + (Int32)f.Element("price"),
ImagePath = (string)f.Element("airlineimageurl").Value
};
There shouldn't be an issue with having more then one condition. For example, you could have something like this from an Order table.
var orderDetails = (from o in context.OrderDetails
where o.OrderID == orderID
where o.OrderName == orderName
select o).ToList();
I have the following linq query:
var totalAmountsPerMonth =
from s in Reports()
where s.ReportDate.Value.Year == year
group s by s. ReportDate.Value.Month into g
orderby g.Key
select new
{
month = g.Key,
totalRecaudacion = g.Sum(rec => rec.RECAUDACION),
totalServicios = g.Sum(ser => ser.SERVICIOS)
};
var final = new ResultSet
{
Recaudacion = meses.Average(q => q. totalRecaudacion),
Servicios = meses.Average(o => o. totalServicios)
};
And I need to obtain the average of the total amount of “RECAUDACION” and “SERVICIOS” of each month. I made this query. However, I definitely think this is not the best solution at all. Could you please suggest me a better and more efficient approach (in a single query if possible) to get these data?
I have created a simple extension method. And it turns out to be two times more efficient in a simple stopwatch benchmark.
public class Report
{
public DateTime? Date { get; set; }
public int RECAUDACION { get; set; }
public int SERVICIOS { get; set; }
}
static class EnumerableEx
{
public static Tuple<double, double> AveragePerMonth(this IEnumerable<Report> reports)
{
var months = new HashSet<int>();
double RECAUDACION = 0d;
double SERVICIOS = 0d;
foreach (Report rep in reports)
{
if (!months.Contains(rep.Date.Value.Month))
{
months.Add(rep.Date.Value.Month);
}
RECAUDACION += rep.RECAUDACION;
SERVICIOS += rep.SERVICIOS;
}
var totalMonth = months.Count;
if (months.Count > 0)
{
RECAUDACION /= totalMonth;
SERVICIOS /= totalMonth;
}
return Tuple.Create<double, double>(RECAUDACION, SERVICIOS);
}
}