我正在每個UserId
,但由於某種原因,foreach
循環......在每一個用戶ID我xml
,只有一個userId
正在與所有的開始時間和結束時間一直顯示userIds
。我需要它分別顯示每個userId
的start
和end
時間。顯示開始時間和結束時間分別
所以它看起來像這樣:
User2
Start
End
Start
End
Start
End
它需要看起來像這樣:
User1
Start
End
Start
End
User2
Start
End
我的代碼:
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 { return userRecordList; }
set { userRecordList = value; }
}
}
public class UserRecord
{
private string userId;
private List<TimePeriod> timePeriodList;
public string UserId
{
get { return userId; }
set { userId = value; }
}
public List<TimePeriod> TimePeriodList
{
get { return timePeriodList; }
set { timePeriodList = value; }
}
}
public class TimePeriod
{
private DateTime startTime;
private DateTime endTime;
public DateTime StartTime
{
get { return startTime; }
set { startTime = value; }
}
public DateTime EndTime
{
get { return endTime; }
set { endTime = value; }
}
}
我的數據從DataAccess
層返回看起來是這樣的:
UserId Time EventType
Test1 xx-xx-xxxx Start
Test1 xx-xx-xxxx End
Test2 xx-xx-xxxx Start
因此,如果有奇數,則結束時間爲User
是DateTime.MinValue
默認。
很好的解釋!謝謝。奇蹟般有效 – user4756836