title: Flatten One-to-Many Relationship Using Dynamic LINQ
body: <p>Is it possible to flatten a one-to-many relationship using dynamic LINQ? </p>
  
  <p>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.</p>
  
  <p>A user will define what types of user preferences are available for a group of users. </p>
  
  <pre><code>public class User
  {
      public string FirstName
      {
          get;
          set;
      }
  
      public string LastName
      {
          get;
          set;
      }
  
      public IList&lt;UserPreference&gt; UserPreferences
      {
          get;
          set;
      }
  }
  
  public class UserPreference
  {
      public UserPreference(string name, object userValue)
      {
          this.Name = name;
          this.UserValue = userValue;
      }
  
      public string Name
      {
          get;
          set;
      }
  
      public object UserValue
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>Therefore one user group might be defined in the following way:</p>
  
  <pre><code>List&lt;User&gt; users = new List&lt;User&gt;();
  
  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></pre>
  
  <p>The desired output would be:</p>
  
  <pre><code>First Name      Last Name       Favorite Color      Favorite Mammal
  John        Doe         Red         NULL
  Jane        Doe         Blue            Dolphin
  </code></pre>
  
  <p>Is there a way to create an anonymous type so that UserPreferences would get rolled up into the User? </p>
  
  <p>For example,</p>
  
  <pre><code>var u = UserScopedSettingAttribute.Select("new (FirstName as FirstName, UserValue as FavoriteColor)", null);
  
  string name = u.FirstName;
  string color = u.FavoriteColor;
  </code></pre>
  
  <p>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.</p>
  
  <p>Any suggestions are appreciated! </p>