/// <summary> /// Calls GetContacts(int) to generate a list of randomly constructed Contact objects and groups /// them into an ObservableCollection of GroupInfoList objects for binding in the ZoomedInView and /// ZoomedOutView of the SemanticZoom control. This function is not called in this demo, but is provided /// as an example. It will only generate GroupInfoList objects with Contact members and will not make any Empty /// GroupInfoList objects. Call this if you don't want to have Empty GroupInfoList members (maybe...remember that the Contact /// objects are generated randomly so there might be a Contact for every Key/letter), and then your ZoomedOutView /// will only have GroupInfoList objects representing those Keys (letters) with Contacts in them. /// </summary> /// <param name="numberOfContacts"></param> /// <returns>An ObservableCollection of GroupInfoList objects containing Contact objects.</returns> public static ObservableCollection <GroupInfoList> GetContactsGrouped(int numberOfContacts) { ObservableCollection <GroupInfoList> groups = new ObservableCollection <GroupInfoList>(); // This Linq expression calls GetContacts for a bunch of randomly-generated Contact objects, // groups them by the first letter of the LastName of each Contact, orders them alphabetically, and makes an // anonymous type object containing the GroupName (key) and the list of items (Contacts). var query = from item in GetContacts(numberOfContacts) group item by item.LastName[0] into g orderby g.Key select new { GroupName = g.Key, Items = g }; // This iterates through the query results and makes a GroupInfoList object for each // group of Contacts represented by a letter. foreach (var g in query) { GroupInfoList info = new GroupInfoList(); // set the Key property of the GroupInfoList to the GroupName of the anonymous type object // generated in the Linq query above info.Key = g.GroupName.ToString(); // iterate through all the Contact items in the Linq query and add them // to the GroupInfoList object derived from List<T> foreach (var item in g.Items) { info.Add(item); } // add this GroupInfoList to the ObservableCollection<GroupInfoList> that we will return groups.Add(info); } return(groups); }
/// <summary> /// Like GetContactsGrouped, this generates an ObservableCollection of GroupInfoLists, but does it a bit /// differently. For Contact groups that have no Contact data members, this will generate an empty GroupInfoList /// object representing the letter (or number), and allow you to display a ZoomedOutView just like the Contacts list /// on Windows Phone, where empty groups with no Contacts are grayed out and not selectable. /// </summary> /// <param name="numberOfContacts"></param> /// <returns>An ObservableCollection of GroupInfoList objects containing Contact objects.</returns> public static ObservableCollection <GroupInfoList> GetContactsGroupedAllAlpha(int numberOfContacts) { ObservableCollection <GroupInfoList> groups = new ObservableCollection <GroupInfoList>(); // get an ObservableCollection of Contact objects (it could also be a List<Contact>). var generatedContacts = GetContacts(numberOfContacts); // the letters/numbers representing our GroupInfoList Keys var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToList(); // This Linq expression creates an IEnumerable collection of anonymous types containing the letter/number // Key of the group, and its associated Contact objects by iterating through the List of letters/numbers // and getting the Contact objects where the LastName property starts with that letter/number, ordering them // by LastName. var groupByAlpha = from letter in letters select new { Key = letter.ToString(), query = from item in generatedContacts where item.LastName.StartsWith(letter.ToString(), StringComparison.CurrentCultureIgnoreCase) orderby item.LastName select item }; // Now, we create the GroupInfoList objects and add them to our returned ObservableCollection. // iterate through the IEnumerable 'groupByAlpha' created above foreach (var g in groupByAlpha) { // make a new GroupInfoList object GroupInfoList info = new GroupInfoList(); // assign its key (letter/number) info.Key = g.Key; // iterate through all the Contact objects for that Key and add them to the GroupInfoList. If the // Key has no Contacts, then none will be added and that GroupInfoList will be empty. foreach (var item in g.query) { info.Add(item); } // add the GroupInfoList to the ObservableCollection to be returned groups.Add(info); } return(groups); }