开发者

How to select static text with LINQ query?

I have a listbox and I want its ItemsSource to be all the items of an ObservableCollection + an additional static text such as "All Items."

Right now I just have something similar to:

listbox1.ItemsSo开发者_StackOverflow社区urce = from Car c in Cars
                       select c.Model

I know I could just manually add the text to the listbox, but I want it to be part of the query because the linq query is bound using the Obtics library (so the UI updates reactively). I am not too familiar with linq queries outside the basics, so does anyone know if this is possible? Thanks


You can use the Union() operator to add more objects into your selected set.

string[] additionalItems = {"All Items"};

listbox1.ItemsSource = (from Car c in Cars
                       select c.Model)
                       .Union(additionalItems);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜