我在可觀察集合中遇到ForEach問題。 Visual Studio告訴我:ForEach在observablecollection中的錯誤
「'observablecollection'不包含'ForEach'定義,並且沒有在'ForEach'擴展方法中找到,它接受第一個'observablecollection'類型參數。可能缺少使用or的指令參考大會「。
問題是,在另一個項目中的類似代碼工作正常,但在這個新項目中沒有。我錯在哪裏?
謝謝
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProvaBindingXaml
{
/* public class Impiegato
{
public string Nome { get; set; }
public string Cognome { get; set; }
public string Matricola { get; set; }
}
*/
public class Rule
{
public string Ruolo { get; set; }
public Rule(string ruolo)
{
this.Ruolo = ruolo;
}
public override string ToString()
{
return this.Ruolo;
}
public static ObservableCollection<Rule> GetAllRules()
{
var CollectionRuoli = new ObservableCollection<Rule>();
CollectionRuoli.Add(new Rule ("Impiegato"));
CollectionRuoli.Add(new Rule ("Dirigente"));
CollectionRuoli.Add(new Rule ("Operaio"));
CollectionRuoli.Add(new Rule ("Manutentore"));
CollectionRuoli.Add(new Rule ("Presidente"));
return CollectionRuoli;
}
public static void GetComboBoxList(ObservableCollection<Rule> RuleItems)
{
var allItems = GetAllRules();
RuleItems.Clear();
allItems.ForEach(p => RuleItems.Add(p));
}
}
}
也許幫助:https://stackoverflow.com/a/2519433/316799 –