2016-02-14 182 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

相关问题