Flatten One-to-Many Relationship Using Dynamic LINQ

Francesc

New Member
Is it possible to flatten a one-to-many relationship using dynamic LINQ? For example, I might have a list of Users and the User class contains a list of many UserPreferences. The UserPreference class is essentially a name/value pair.A user will define what types of user preferences are available for a group of users. \[code\]public class User{ public string FirstName { get; set; } public string LastName { get; set; } public IList<UserPreference> UserPreferences { get; set; }}public class UserPreference{ public UserPreference(string name, object userValue) { this.Name = name; this.UserValue = http://stackoverflow.com/questions/864106/userValue; } public string Name { get; set; } public object UserValue { get; set; }}\[/code\]Therefore one user group might be defined in the following way:\[code\]List<User> users = new List<User>();User user1 = new User();user1.FirstName = "John";user1.LastName = "Doe";user1.UserPreferences.Add(new UserPreference("Favorite color", "Red"));User user2 = new User();user2.FirstName = "Jane";user2.LastName = "Doe";user2.UserPreferences.Add(new UserPreference("Favorite mammal", "Dolphin"));user2.UserPreferences.Add(new UserPreference("Favorite color", "Blue"));users.Add(user1);users.Add(user2);return users;\[/code\]The desired output would be:\[code\]First Name Last Name Favorite Color Favorite MammalJohn Doe Red NULLJane Doe Blue Dolphin\[/code\]Is there a way to create an anonymous type so that UserPreferences would get rolled up into the User? For example,\[code\]var u = UserScopedSettingAttribute.Select("new (FirstName as FirstName, UserValue as FavoriteColor)", null);string name = u.FirstName;string color = u.FavoriteColor;\[/code\]Ultimately this list of Users will get bound to an ASP.NET GridView web control. There will be a large volume of data involved in this operation and performance will be critical.Any suggestions are appreciated!
 
Top