tee*_*pee 4 c# methods type-conversion
我有一个方法返回一个List<DateTime>但是我在方法中运行的代码给了我一个List<string>
如何将我的线路转换List<string>为a ?List<DateTime>return
它基本上是一种方法,它接收照片列表并返回其日期拍摄属性的列表;
//retrieves the datetime WITHOUT loading the whole image
public List<DateTime> GetDateTakenFromImage(List<string> path)
{
List<string> dateTaken = new List<string>();
int cnt = 0;
while (cnt < path.Count())
{
using (FileStream fs = new FileStream(path[cnt], FileMode.Open, FileAccess.Read))
using (Image myImage = Image.FromStream(fs, false, false))
{
PropertyItem propItem = myImage.GetPropertyItem(36867);
dateTaken[cnt] = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
}
cnt++;
}
return dateTaken;
Run Code Online (Sandbox Code Playgroud)
我从网站上获得了这些代码,所以我不确定是否可以让它返回到 List<DateTime>
我想我可以使提供的答案适用于此代码.
谢谢!
mfa*_*nto 18
如果您知道所有字符串都将正确解析为DateTime,您可以执行以下操作:
List<string> strings = new List<string>() { "2014-01-14" };
List<DateTime> dates = strings.Select(date => DateTime.Parse(date)).ToList();
Run Code Online (Sandbox Code Playgroud)