我有类似的代码

IEnumerable<TwitterStatus> mentions = service.ListTweetsOnHomeTimeline();
RecentTweetList.ItemsSource = mentions;


我想做的是向TwitterStatus 1的额外属性中添加某种方式,以将该类的CreatedDate转换为分钟。 (我无法重新设计TwitterStatus。)

所以我想这样做:

public class NewTwitterStatus : TwitterStatus
{
        public string MinAgo
        {
            get
            {
                TimeSpan diff = DateTime.Now.Subtract(base.CreatedDate);
                return diff.Minutes.ToString() + "m";
            }
        }
}


但是我不知道该怎么做演员?

IEnumerable<NewTwitterStatus > mentions = (????)service.ListTweetsOnHomeTimeline();




谢谢大家!

解决方案(基于克里斯的建议):

IEnumerable<NewTwitterStatus> mentions = service.ListTweetsOnHomeTimeline().Select(x => new NewTwitterStatus(x));
RecentTweetList.ItemsSource = mentions;



 public class NewTwitterStatus
    {
        TwitterStatus Data { set; get; }

        public NewTwitterStatus(TwitterStatus ts)
        {
            Data = ts;
            ts = null;
        }

        public string ProfileImageUrl
        {
            get
            {
                return Data.User.ProfileImageUrl;
            }
        }


        public string ScreenName
        {
            get
            {
                return Data.User.ScreenName;
            }
        }

        public string UserName
        {
            get
            {
                return Data.User.Name;
            }
        }

        public string Text
        {
            get
            {
                return Data.Text;
            }
        }

        public string MinAgo
        {
            get
            {
                TimeSpan diff = DateTime.Now.Subtract(Data.CreatedDate);
                int mins = diff.Minutes * -1;

                if (mins < 60)
                    return mins.ToString() + "m";
                else
                {
                    double h = mins / 60;
                    double m = mins % 60;
                    return h.ToString()  + "h " + m.ToString() + "m";
                }


            }
        }

最佳答案

如果您不能执行扩展方法,那么我将使您的NewTwitterStatus类具有一个采用TwitterStatus的构造函数,以方便进行转换,然后使用如下所示的linq:

IEnumerable<NewTwitterStatus > mentions =
    service.ListTweetsOnHomeTimeline().Select(x=> new NewTwitterStatus(x));


这基本上将采用原始的IEnumberable并将元素映射到新类型。它没有扩展方法那么优雅,但是应该可以解决问题。

10-08 00:04