[英]How to convert List<string> to Dictionary<string,string> using linq?
[英]How to convert string[] to List<UserType>, using linq?
// 示例: path = "C:\Users\MSI\Desktop\1.jpg"
There is an array and list
:
string[] files = new string[] {"path1", "path2", "path3"};
List<UserType> mainFileList = new List<UserType>();
我不想每次都描述整个循环(for、foreach、...),我想将数据从 string[] 转换为简短形式的 List。 如何使用 Linq 做到这一点?
public class UserType{
// Public fields
public readonly string FullPath;
public readonly string Name;
public UserType() { }
public UserType(string fullPath) {
FullPath = fullPath;
Name = Path.GetFileName(fullPath);
}
}
您可以使用 LINQ:
string[] files = new string[] {"path1", "path2", "path3"};
List<UserType> mainFileList = files.Select(f => new UserType(f)).ToList();
但实际上,还是会有一个循环,现在隐藏在Select
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.