Question

I have module which is not mapped to database( sql server) and is only use to generate report.

public class Report
{
    public int USERID { get; set; }
    public DateTime DateToCal { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public TimeSpan? Intime { get; set; }
    public TimeSpan?  OutTime { get; set; }
}

I generate a query and fill some properties(USERID, DateToCal, Name, Position, Intime) of Report and some properties of Report remains null ( as OutTime is null)

var query = .....;

Now what I want iterate on items of query( of type Report) and set value for null properties OutTime as

foreach(var items in query)
            {
                var outtime= from x in con.CHECKINOUTs
                              where x.USERID == items.USERID && EntityFunctions.TruncateTime(x.CHECKTIME) == EntityFunctions.TruncateTime(items.DateToCal && x.CHECKTYPE == "O"
                              select x.CHECKTIME
                              .Single();
                items.OutTime= outtime.TimeOfDay;
            }

Now problem is, on mousehover to items.OutTime with in foreach there appear value but if I out from foreach and mousehover to query there is still OutTime is null. There not appear value what I set. Is this is possible to set value of entities such way. Or what is my problem?

Thank you.

Was it helpful?

Solution

Save query results locally before iterating over them:

 var query = ....ToList();

Looks like in your case query executed two times - first time when you are updating OutTime property, and second time when you are iterating over query items (either looking in debugger or displaying in UI). So, when query executed second time, you see completely new set of objects as query result (which have original null values of OutTime).

BTW Consider to use single JOIN query instead of making separate outtime query for each item in your main query.

OTHER TIPS

Try code something like this:

public class Report
{
    public int USERID { get; set; }
    public DateTime DateToCal { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    private TimeSpan? _intime;
    public TimeSpan Intime {
        get { return _intime ?? new TimeSpan(0); }  
        set { _intime = value; }
    }
    private TimeSpan? _outTime;
    public TimeSpan OutTime
    {
        get { return _outTime ?? new TimeSpan(0); }
        set { _outTime = value; }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top