title: How to use factory classes with linq for sql?
body: <p>I have a model on top of my database model and map the objects in my Repository.</p>
  
  <p>However, apparently it makes a difference whether I "select new" directly in my GetUsers or "select factoryresult" as implemented below. I get the error at runtime, that the method CreateFromDbModel does not have a translation to sql (System.NotSupportedException).</p>
  
  <p>Is there a way around this? Can I mend it somehow?</p>
  
  <p>The reason for wanting to use the factory method is that I might instanciate objects elsewhere and want to keep the 'mapping code' in one place...</p>
  
  <p>Thanks for any comments,
  Anders</p>
  
  <pre><code>    public IQueryable&lt;User&gt; GetUsers(bool includeTeams)
      {
          return from u in _db.sc_Players
                 where (includeTeams || (!u.aspnet_User.sc_Player.IsTeam))
                 select UserFactory2.CreateFromDbModel(u);
      }
  
  
      public static User CreateFromDbModel(sc_Player player)
      {
          return new User
                     {
                         Id = player.sc_PlayerID,
                         FirstName = player.FirstName.Trim(),
                         LastName = player.LastName.Trim(),
                         PresentationName = player.FirstName.Trim() + " " + player.LastName.Trim(),
                         LoginName = player.aspnet_User.LoweredUserName,
                         IsTeam = player.IsTeam,
                         Email = player.aspnet_User.aspnet_Membership.Email,
                         Password = player.aspnet_User.aspnet_Membership.Password
                     };
      }
  </code></pre>