【问题标题】:Why is the return is List<char>?为什么返回的是 List<char>?
【发布时间】:2019-12-09 10:52:37
【问题描述】:

我正在尝试使用“包含”方法提取与子字符串匹配的文件名。然而,回报似乎是List&lt;char&gt;,但我希望List&lt;string&gt;

private void readAllAttribues()
{
    using (var reader = new StreamReader(attribute_file))
    {
        //List<string> AllLines = new List<string>();
        List<FileNameAttributeList> AllAttributes = new List<FileNameAttributeList>();

        while (!reader.EndOfStream)
        {
            FileNameAttributeList Attributes = new FileNameAttributeList();
            Attributes ImageAttributes = new Attributes();
            Point XY = new Point();
            string lineItem = reader.ReadLine();
            //AllLines.Add(lineItem);
            var values = lineItem.Split(',');

            Attributes.ImageFileName = values[1];
            XY.X = Convert.ToInt16(values[3]);
            XY.Y = Convert.ToInt16(values[4]);
            ImageAttributes.Location = XY;
            ImageAttributes.Radius = Convert.ToInt16(values[5]);
            ImageAttributes.Area = Convert.ToInt16(values[6]);
            AllAttributes.Add(Attributes);
        }
        List<string> unique_raw_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).FirstOrDefault().ImageFileName.ToList();
        List<string>var unique_reference_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"ref")).FirstOrDefault().ImageFileName.ToList();


        foreach (var unique_raw_filename in unique_raw_filenames)
        {
            var raw_attributes = AllAttributes.Where(x => x.ImageFileName == unique_raw_filename).ToList();

        }
    }
}

数据类型类

public class FileNameAttributeList
        {   // Do not change the order
            public string ImageFileName { get; set; }
            public List<Attributes> Attributes { get; set; }

            public FileNameAttributeList()
            {
                Attributes = new List<Attributes>();
            }
        }

为什么 FirstOrDefault() 不起作用? (它返回 List&lt;char&gt; 但我期待 List&lt;string&gt; 并且失败了。

【问题讨论】:

  • 你为什么期望x.ToList(),其中xString,产生List&lt;String&gt;
  • FirstOrDefault 应用于Where 查询的结果,这可能是一个可枚举的。因此,您只采用 first 对象的 ImageFileName(可能是一个字符串),然后转换该字符串 ToList 将创建其字符列表。
  • 归结为您将 ToList() 应用于错误的事物。你把它应用到一个字符串上,而不是......无论你当时期望的东西是什么。属性名称是赠品 - ImageFileName听起来像是一个文件名,而不是文件名的集合。 string 而不是 List&lt;string&gt;string[] 或您在这里期望的任何其他内容。

标签: c# list contains


【解决方案1】:

ToList() 方法将实现IEnumerable&lt;SomeType&gt; 的集合转换为列表。 查看Stringdefinition,可以看到它实现了IEnumerable&lt;Char&gt;,所以下面代码中的ImageFileName.ToList()会返回一个List&lt;char&gt;

AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).FirstOrDefault().ImageFileName.ToList();

虽然我猜测您想要什么,但您似乎想根据 ImageFileName 过滤 AllAttributes,然后获取这些文件名的列表。如果是这样的话,你可以使用这样的东西:

var unique_raw_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).Select(y=>y.ImageFileName).ToList();

【讨论】:

    【解决方案2】:

    在你的代码中

    List<string> unique_raw_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).FirstOrDefault().ImageFileName.ToList();
    

    FirstOrDefault() 返回列表 AllAttributes 中的第一个或默认 FileNameAttributeList,其中 ImageFileName 包含文本 non。 在 ImageFileName 上调用 ToList() 然后将字符串值转换为字符列表,因为字符串是字符的集合。

    我认为您的意图可以通过将FirstOrDefault 切换为Select 来实现。 Select 允许您将一个值映射到另一个值。

    所以你的代码可能看起来像这样。

    List<string> unique_raw_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).Select(x => x.ImageFileName).ToList();
    

    这会给你一个字符串列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多