我有一些看起来像这样的代码:
List<ListBoxItem> items = (
from String file in e.Result
select new ListBoxItem {
Content = file.Split('\\').Last(),
Tag = Content,
}).ToList<ListBoxItem>();
Run Code Online (Sandbox Code Playgroud)
这里,在创建对象之后,结果不等同于
List<ListBoxItem> items = (
from String file in e.Result
select new ListBoxItem {
Content = file.Split('\\').Last(),
Tag = file.Split('\\').Last(),
}).ToList<ListBoxItem>();
Run Code Online (Sandbox Code Playgroud)
为什么生成的对象初始化不同?
柯克的回答是正确的.但是,我认为你要做的是使用你的代码更加干燥.我想这就是你要做的事情:
List<ListBoxItem> items = (from String file in e.Result
let lastFile = file.Split('\\').Last()
select new ListBoxItem
{
Content = lastFile,
Tag = lastFile
}).ToList<ListBoxItem>();
Run Code Online (Sandbox Code Playgroud)