【问题标题】:Show start and end time for each User ID respectively分别显示每个用户 ID 的开始和结束时间
【发布时间】:2016-05-25 03:05:33
【问题描述】:

我在每个UserId 上运行foreach 循环,但由于某种原因...在我的xml 中,只有一个userId 与所有@987654325 的所有开始时间和结束时间一起显示@。我需要它分别为每个userId 显示startend 时间。

所以它看起来像这样:

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

【问题讨论】:

    标签: c# list loops for-loop arraylist


    【解决方案1】:

    如果您单步调试程序,注意变量的状态,您可以轻松发现错误。你的代码几乎可以工作,因为你得到了结果。您看到的唯一结果是最后一个用户...

    让我们看看你的循环:

    // 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 有两个项目,Test1 和 Test2。在 foreach 循环上放置一个断点并开始调试。

    如果我们进入 foreach 循环这个状态:

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

    如果我们走到response.UserRecordList = new List&lt;UserRecord&gt;();这一行,这就是状态:

    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 但是如果我们越过那行,就会创建一个 NEW 实例,我将其称为 list2:

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

    通过这一步,之前的 list1 不再被引用,稍后将被垃圾回收。在最后一行添加了 test2 的 UserRecord,然后我们就完成了。

    显然,创建用户记录列表的行不应该在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2015-11-20
      相关资源
      最近更新 更多