简体   繁体   English

如何将所有DateTime属性转换为集合中的其他TimeZone?

[英]How can I convert all DateTime properties to a different TimeZone in a collection?

I have a site that has users, and I have an API method to display a user's latest activity: 我有一个拥有用户的网站,并且有一个API方法来显示用户的最新活动:

public List<UserActivity> GetUserActivity(Guid userId)
{
    return _dbContext.UserActivity
               .Where(ua => ua.UserId.Equals(userId))
               .OrderByDescending(ua => ua.ActivityTime)
               .Take(20)
               .ToList();
}

These are the objects I'm returning: 这些是我要返回的对象:

public class UserActivity
{        
    public Guid UserId { get; set; }
    public DateTime ActivityTime { get; set; }
    public string ActivityTitle { get; set; }
}

I'm storing all my dates for ActivityTime in UTC in my database. 我将ActivityTime所有日期存储在数据库中的UTC中。 Each user can set which timezone they're in, so I'd like to return the ActivityTime converted to their timezone for display on the frontend when this API method is called. 每个用户都可以设置他们所在的时区,因此我想将转换为他们的时区的ActivityTime返回到在调用此API方法时显示在前端的位置。

Something like this: 像这样:

public List<UserActivity> GetUserActivity(Guid userId)
{
    MyUser user = _dbContext.MyUsers.FirstOrDefault(u => u.Id.Equals(userId));
    TimeZoneInfo userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(user.TimeZoneId);

    return _dbContext.UserActivity
               .Where(ua => ua.UserId.Equals(userId))
               .OrderByDescending(ua => ua.ActivityTime)
               .Take(20)
               // somehow call TimeZoneInfo.ConvertTimeFromUtc on ActivityTime here
               .ToList();
}

But I'm not sure how to call TimeZoneInfo.ConvertTimeFromUtc on a property in a collection to convert all of them. 但是我不确定如何在集合中的属性上调用TimeZoneInfo.ConvertTimeFromUtc来转换所有属性。 Is there a way to do that? 有没有办法做到这一点?

Once you have all your UserActivity data in memory, then you can convert it. 一旦将所有UserActivity数据存储在内存中,就可以对其进行转换。

var activities = ...;
TimeZoneInfo userZone = TimeZoneInfo.FindSystemTimeZoneById(user.TimeZoneId);
foreach (var thisActivity in activities)
{
    DateTime localTime = 
       TimeZoneInfo.ConvertTimeFromUtc(thisActivity.ActivityTime, userZone);
    thisActivity.ActivityTime = localTime;
}

The important thing you need to do is to convert your DateTime entities after calling ToList() for your EntityFramework DbSet. 您需要做的重要事情是在为EntityFramework DbSet调用ToList() 之后转换DateTime实体。

Here is an example using just LINQ by projecting your entities: 这是通过投影实体而仅使用LINQ的示例:

public List<UserActivity> GetUserActivity(Guid userId)
{
    MyUser user = _dbContext.MyUsers.FirstOrDefault(u => u.Id.Equals(userId));
    TimeZoneInfo userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(user.TimeZoneId);

    var activities = _dbContext.UserActivity
                               .Where(ua => ua.UserId.Equals(userId))
                               .OrderByDescending(ua => ua.ActivityTime)
                               .Take(20)
                               .ToList();

    return activities.Select(a => new UserActivity
    {
        UserId = a.Userid,
        ActivityTitle = a.ActivityTitle,
        ActivityTime = TimeZoneInfo.ConvertTimeFromUtc(a.ActivityTime, userTimeZone)
    }).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM