I have got code something like
IEnumerable<TwitterStatus> mentions = service.ListTweetsOnHomeTimeline();
RecentTweetList.ItemsSource = mentions;
What I want to do is to add somehow to TwitterStatus 1 extra attribute to convert CreatedDate of that class to get minutes. (I cannot redesign TwitterStatus.)
So I guessed to do like:
public class NewTwitterStatus : TwitterStatus
{
public string MinAgo
{
get
{
TimeSpan diff = DateTime.Now.Subtract(base.CreatedDate);
return diff.Minutes.ToString() + "m";
}
}
}
But I cannot figure out how to do the cast?
IEnumerable<NewTwitterStatus > mentions = (????)service.ListTweetsOnHomeTimeline();
Thank you all of you guys!
THE SOLUTION (based on Chris’ suggestion):
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";
}
}
}
If you can’t do an extension method then I would make your
NewTwitterStatusclass have a constructor that takes aTwitterStatusto allow easy conversion and then use some linq like the following:This will basically take the original IEnumberable and map the elements into the new type. Its a bit less elegant than an extension method but should do the trick.