简体   繁体   中英

how to convert list to IEnumerable<string>?

Error CS1503 Argument 2: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable

public async System.Threading.Tasks.Task SerializeAsync(List<ProgrammInfo> list)
{
    using (FileStream fs = File.AppendAllLines("save3.json", list))
    {
        var options = new JsonSerializerOptions
        {
            WriteIndented = true
        };
        await JsonSerializer.SerializeAsync(fs, list, options);
    }
}

The reason is that the variable list is not a list of strings but a list of ProgramInfo. By using Linq You can change the code like this:

        public async System.Threading.Tasks.Task SerializeAsync(List<ProgrammInfo> list)
        {

            var stringList = list.Select(x => x.ToString());

            using (FileStream fs = File.AppendAllLines("save3.json", stringList))
...

But I could not make the code run since AppendAllLines return void ... and to serialize an object to json and write it to a file can be done easier as Knoop commented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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