使用分组从IList创建ObservableCollection

时间:2014-12-29 20:21:09

标签: c# observablecollection

我试图将分组的可观察集合传递给我的视图。

public void init()
{
    people = CreatePeople( 20 ).ToList(); // Creates 20 person-objects

    groupedPeople = ( from person in people
    group person by person.City into g
    orderby g.Key
    select new Group<string, Person>
    {
        Key = g.Key.ToString(),
        Items = g.ToList()
    } ).ToList();

}

private IList<Person> people;
private IList<Group<string, Person>> groupedPeople; //Contains the groupedlist that i want to "convert"

public ObservableCollection<Group<string, Person>> gp;  //This is the list I wish to pass to the view

为了让我通过列表,我需要在ObservableCollection

public ObservableCollection<Group<string, Person>> gp;

我可以将private IList<Group<string, Person>>“转换”为observableCollection吗?

1 个答案:

答案 0 :(得分:1)

  gp=new ObservableCollection(groupedPeople);

ObservableCollection的构造函数可以使用IList或IEnumerable