I have a list of 500+ EventData records. Model of EventData is as given below
public class EventData
{
public int preEventId { get; set; }
public int empNum { get; set; }
public int EventId { get; set; }
public DateTime CreateDate { get; set; }
public string UserId { get; set; }
}
Here the EventId can be 1,2,3,4 and 5 (Enum). and from this list, I want to filter the records for all weeks of the month and then calculate the avg. NB: weekList is the list of Mondays in the current month.
(count of records having EventId = 1) / (count of records with EventId = 2 or 5 and empNum's with EventId = 1 (CreateDate 5 days old or newer))
What I've done is:-
int calledCount = lstEventData?.Where(e => e.EventId == 1 &&
e.CreateDate >= weekList.ToList()[week] &&
e.CreateDate <= weekList.ToList()[week].AddDays(4)).Count() ?? 0;
int totalCount = (lstEventData?
.Where(e => (e.EventCd == 2 || e.EventCd == 5) &&
e.CreateDate >= weekList.ToList()[week] &&
e.CreateDate <= weekList.ToList()[week].AddDays(4))
.GroupBy(e => e.empNum)
.Select(x => x.First())
.Count()) ?? 0;
int avgCalls = Convert.ToDecimal(calledCount) / Convert.ToDecimal(totalCount);
Sample SQL structure of the same:
SELECT * FROM CalledList c INNER JOIN
Events e
ON (c.empNum = e.empNum AND c.EventId IN(2,5)
AND c.CreateDate > e.CreateDate - 5
AND c.EventId = 1
How can I achieve my actual goal?