为什么返回的是List <char>?

PCG*_*PCG 0 c# contains list

我正在尝试使用“包含”方法提取与子字符串匹配的文件名。但是,回报似乎是List<char>但我期望List<string>

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();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

数据类型类

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>();
            }
        }
Run Code Online (Sandbox Code Playgroud)

为什么FirstOrDefault()不起作用?(它返回了,List<char>但是我期望会List<string>失败。

Kol*_*kov 5

ToList()方法将实现的集合转换IEnumerable<SomeType>为列表。纵观定义String,你可以看到它实现IEnumerable<Char>,所以ImageFileName.ToList()在下面的代码将返回List<char>

AllAttributes.Where(x => x.ImageFileName.Contains(@“ non”))。FirstOrDefault()。ImageFileName.ToList();

虽然我在想您要什么,但似乎您想根据ImageFileName筛选AllAttribute,然后获取这些文件名的列表。如果是这样,您可以使用类似以下的方法:

var unique_raw_filenames = AllAttributes.Where(x => x.ImageFileName.Contains(@"non")).Select(y=>y.ImageFileName).ToList();
Run Code Online (Sandbox Code Playgroud)