R
R
Roman Sokolov2014-08-05 13:47:17
C++ / C#
Roman Sokolov, 2014-08-05 13:47:17

How to correctly correlate a calculated field of an Entity Framework model?

Hello!
There is some calculated field in the Entity Framework model:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
            [NotMapped]
            public int? WorkingTime
            { get {
                    var query = (from e in context.events
                                 join a in context.actions on e.CurrEventID equals a.CurrEventID into es
                                 from p in es.DefaultIfEmpty()
                                 select new { UnitDate = e.ActionDate, UnitId = e.CurrEventID, HeaderID = p.HeaderID, IsDone = p.MarkerOn, Begin = e.BeginTime, End = e.EndTime })
                                .Where(w => w.HeaderID == this.HeaderID)
                                .Where(w => w.IsDone == true)
                                .Distinct()
                                .Select(s =>  DbFunctions.DiffMinutes(s.Begin, s.End)).Sum();
                    return query ?? 0;
               } }

For each HeaderID, WorkingTime is calculated for all rows in the database. You must enter an external limit on the range of requested dates (the current date is stored in the UnitDate field). The model is stored separately from the client from which the call is made. Tell me how to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoliy Mikhailov, 2014-10-13
@yamaoto

Implement as a helper?

public static class Helper{
public static int? GetWorkingTime(this MyContext context){
var query = (from e in context.events
                                 join a in context.actions on e.CurrEventID equals a.CurrEventID into es
                                 from p in es.DefaultIfEmpty()
                                 select new { UnitDate = e.ActionDate, UnitId = e.CurrEventID, HeaderID = p.HeaderID, IsDone = p.MarkerOn, Begin = e.BeginTime, End = e.EndTime })
                                .Where(w => w.HeaderID == this.HeaderID)
                                .Where(w => w.IsDone == true && w.Begin<w.UnitDate && w.UnitDate<w.End )
                                .Distinct()
                                .Select(s =>  DbFunctions.DiffMinutes(s.Begin, s.End)).Sum();
                    return query ?? 0;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question