简体   繁体   English

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

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

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

There is an array and list : There is an array and list

  1. string[] files = new string[] {"path1", "path2", "path3"};
  2. List<UserType> mainFileList = new List<UserType>();

I don't want to describe the whole loop (for, foreach, ...) every time, I want to convert data from string[] to List in short form.我不想每次都描述整个循环(for、foreach、...),我想将数据从 string[] 转换为简短形式的 List。 How to do it using Linq?如何使用 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);
  }
}

You can use LINQ:您可以使用 LINQ:

string[] files = new string[] {"path1", "path2", "path3"};
List<UserType> mainFileList = files.Select(f => new UserType(f)).ToList();

But actually, there will still be a loop, which is now hidden in the Select method.但实际上,还是会有一个循环,现在隐藏在Select方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换List <string> 到字典 <string,string> 使用linq? - How to convert List<string> to Dictionary<string,string> using linq? 如何使用LINQ将List <string>中的所有字符串转换为小写? - How to Convert all strings in List<string> to lower case using LINQ? 使用LINQ将列表转换为CSV字符串 - Using LINQ to convert a list to a CSV string 使用linq将逗号分隔的字符串转换为列表 - convert comma separated string to list using linq 如何转换清单 <string> 列出 <int> 在C#中使用LINQ将空或空字符串转换为0的地方 - How to convert List<string> to List<int> where empty or null string to be converted as 0 using LINQ in C# 如何将“int”数据类型项转换为“字符串”并使用 LINQ 中的“包含”在字符串列表中搜索? - How to convert an 'int' data type item to 'string' and search in a string list using 'Contains' in LINQ? 将&#39;ArrayList&#39;转换为&#39;List <string> &#39;(或&#39;清单 <T> &#39;)使用LINQ - Convert 'ArrayList' to 'List<string>' (or 'List<T>') using LINQ 如何将日期转换为字符串并使用Linq进行比较 - How to convert date to string and compare using Linq 在C#中使用LINQ转换List <string> 到列表 <char> - using LINQ in C# to convert a List<string> to a List<char> 如何使用LINQ将ResourceSet转换为名称列表? - How to convert a ResourceSet to a List of names using LINQ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM