我正在使用下面的C#代码填充WinForms ListBox。我想隐藏所有系统文件夹。比如$ RecyclingBin。但它给了我以下错误。
System.ArgumentException:复杂的DataBinding接受IList或IListSource作为数据源。
对LINQ不熟悉这对我来说不仅仅是让人困惑。谁能告诉我哪里出错了?
string[] dirs = Directory.GetDirectories(@"c:\");
var dir = from d in dirs
where !d.StartsWith("$")
select d;
listBox.DataSource = (dir.ToString());
答案 0 :(得分:23)
变化:
listBox.DataSource = (dir.ToString());
要:
listBox.DataSource = dir.ToList();
dir.ToString()
只会吐出一些可枚举的描述,这是无用的。错误消息表明它需要一个列表,因此需要.ToList()
。