I am ruing a foreach loop on each UserId but for some reason... In my xml, only one userId is being displayed along with all the start time and end time for all userIds. I need it to show start and end time for each userId respectively.
So it looks like this:
User2
Start
End
Start
End
Start
End
It needs to looks like this:
User1
Start
End
Start
End
User2
Start
End
My code:
foreach (String userId in request.Users) // request.Users is a Array of UserId's
{
List<UserModel> result = // I am making my DataAccess Layer call here.
UserRecord record = new UserRecord();
record.UserId = userId;
record.TimePeriodList = new List<TimePeriod>();
for (int i = 0; i < result.Count; i += 2)
{
TimePeriod timeData = new TimePeriod();
timeData.StartTime = result[i].TimeDate;
// if result count is an odd number and this is the last iteration for the UserId
if (((result.Count & 1) == 1) && (i == result.Count))
{
timeData.EndTime = result[i + 1].TimeDate;
}
record.TimePeriodList.Add(timeData);
}
response.UserRecordList = new List<UserRecord>();
response.UserRecordList.Add(record);
}
public class GetUserResponse
{
private List<UserRecord> userRecordList;
public List<UserRecord> UserRecordList
{
get { retu userRecordList; }
set { userRecordList = value; }
}
}
public class UserRecord
{
private string userId;
private List<TimePeriod> timePeriodList;
public string UserId
{
get { retu userId; }
set { userId = value; }
}
public List<TimePeriod> TimePeriodList
{
get { retu timePeriodList; }
set { timePeriodList = value; }
}
}
public class TimePeriod
{
private DateTime startTime;
private DateTime endTime;
public DateTime StartTime
{
get { retu startTime; }
set { startTime = value; }
}
public DateTime EndTime
{
get { retu endTime; }
set { endTime = value; }
}
}
My data retued from DataAccess layer looks like this:
UserId Time EventType
Test1 xx-xx-xxxx Start
Test1 xx-xx-xxxx End
Test2 xx-xx-xxxx Start
So if there is an odd number then the End time for that User is DateTime.MinValue by default.
