为每个observablecollection项添加一个新字符串

时间:2014-05-31 11:00:20

标签: c# list observablecollection

我有一个类型库存的可观察集合 定义为

Observablecollection<Inventory> inventory = new ObservableCollection<Inventory>();

库存有:

inventory.name;
inventory.status;
inventory.place;

如何在每个库存的索引[0]处添加一些字符串,如下所示:

["icescream", inventory.name, inventory.status, inventory.place];
["popcorn", inventory.name, inventory.status, inventory.place];
["popcorn", inventory.name, inventory.status, inventory.place];
["lollipop", inventory.name, inventory.status, inventory.place];
["candy", inventory.name, inventory.status, inventory.place];

这可能吗?

1 个答案:

答案 0 :(得分:0)

合适的linq表达式可能如下所示:

var joined = inventory.Select(item => new {value = "Test " + item.name, item});
// than for e.g. you could iterate through and print them out
foreach (var anonymousItem in joined)
{
    var item = anonymousItem.item;

    const string format = "[\"{0}\", {1}, {2}, {3}];";
    Console.WriteLine(format, anonymousItem.value, item.name, item.status, item.place);
}