Cast ListView Items to List?

后端 未结 4 818
[愿得一人]
[愿得一人] 2021-02-13 11:02

How can I cast ListView.Items to a List?

This is what I tried:

List list = lvFiles.Items.Cast

        
4条回答
  •  我在风中等你
    2021-02-13 11:23

    The Cast method will essentially try to perform a box/unbox, so it will fail if the items in the list aren't already strings. Try this instead:

    List list = lvFiles.Items.Cast()
                                     .Select(x => x.ToString()).ToList();
    

    Or this

    List list = lvFiles.Items.Cast()
                                     .Select(x => x.Text).ToList();
    

提交回复
热议问题