2016-02-14 69 views
0

我正在每個UserId,但由於某種原因,foreach循環......在每一個用戶ID我xml,只有一個userId正在與所有的開始時間和結束時間一直顯示userIds。我需要它分別顯示每個userIdstartend時間。顯示開始時間和結束時間分別

所以它看起來像這樣:

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 

因此,如果有奇數,則結束時間爲UserDateTime.MinValue默認。

回答

2

如果您逐步瀏覽程序,注意變量的狀態,您可以輕鬆發現錯誤。你的代碼幾乎可以工作,因爲你得到了結果。你見得到的唯一的結果就是最後一個用戶...

讓我們看看你的循環:

// you have created a response object here which I had to infer 
var response = new GetUserResponse(); // added so my explanation makes sense 

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(); 
    // removed stuff that already works 
    response.UserRecordList = new List<UserRecord>(); 
    response.UserRecordList.Add(record); 
} 

request.Users有兩個項目,測試1和Test2的。在foreach循環中放一個斷點並開始調試。

如果我們步入foreach循環這種狀態:

response = instance1 of GetUserResponse 
response.UserRecordList = null; 
userId = test1 

如果我們一步直到該行response.UserRecordList = new List<UserRecord>();這是國家:

response = instance1 of GetUserResponse 
response.UserRecordList = null; 
userId = test1 
record = instance1 of UserRecord with values in its fields 

當我們跨過下一行的狀態變成

response.UserRecordList = new instance of List<UserRecord>(); // list1 

並且在循環中的最後一行之後我們的狀態是

response.UserRecordList.Length = 1 

現在我們從數組中取下一項,所以userId變成了test2。 讓我們再次運行,直到之前的最後一行,到現在爲止UserRecordList仍然有1 UserRecord但如果我們跨過這道線,實例被創建,我指的是爲列表2:

response.UserRecordList = new instance of List<UserRecord>(); // list2 

通過這種先前的列表1不再被引用,並且稍後將被垃圾收集。在最後一行添加test2的UserRecord,然後我們完成了。

顯然,創建UserRecords列表的行不應位於foreach循環內。

解決方法是相當容易

var response = new GetUserResponse(); // added so my explanation makes sense 
response.UserRecordList = new List<UserRecord>(); // init List once 

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(); 
    // removed stuff that already works 
    // don't init the UserRecordList here, it is done at the start of the loop. 
    response.UserRecordList.Add(record); 
} 
+0

很好的解釋!謝謝。奇蹟般有效 – user4756836

相關問題