繁体   English   中英

如何将字符串 [] 转换为列表<usertype> , 使用 linq?</usertype>

[英]How to convert string[] to List<UserType>, using linq?

// 示例: path = "C:\Users\MSI\Desktop\1.jpg"

There is an array and list

  1. string[] files = new string[] {"path1", "path2", "path3"};
  2. 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.

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