title: Entity Framework condition in select
body: <p>I have the following database code:</p>
  
  <pre><code>static IEnumerable&lt;dynamic&gt; GetData(bool withchildren) {
      using (var model = new testEntities()) {
          var res = default(IQueryable&lt;dynamic&gt;);
          if (withchildren) {
              res = model.UserSet
                  .Where(u =&gt; u.name != "")
                  .Select(u =&gt; new {
                      Name = u.name,
                      Email = u.email,
                      Groups = u.GroupSet.Select(g =&gt; new {
                          Name = g.name,
                          Id = g.Id
                      })
                  });
          } else {
              res = model.UserSet
                  .Where(u =&gt; u.name != "")
                  .Select(u =&gt; new {
                      Name = u.name,
                      Email = u.email
                  });
          }
          return res.ToList()
      }
  }
  </code></pre>
  
  <p>I would like to shrink the code and write it like this:</p>
  
  <pre><code>static IEnumerable&lt;dynamic&gt; GetData(bool withchildren) {
      using (var model = new testEntities()) {
          var res = default(IQueryable&lt;dynamic&gt;);
          res = model.UserSet
              .Where(u =&gt; u.name != "")
              .Select(u =&gt; {
                  dynamic item = new { 
                      Name = u.name,
                      Email = u.email
                  };
                  if(withchildren) {
                      item.Groups = u.GroupSet.Select(g =&gt; new {
                          Name = g.name,
                          Id = g.Id
                      });
                  }
                  return item;
              });
          return res.ToList();
      }
  }
  </code></pre>
  
  <p>But Visual Studio complains, that it cannot convert the lambda expression into an expression tree.</p>
  
  <p>My question is, is there a way to accomplish that with the Entity Framework and Linq? I really wouldn't want to use ADO.net directly.</p>
  
  <p>Maybe there is even a better version to shrink it, than the code that I imagine.</p>
  
  <p><a href="https://stackoverflow.com/questions/8253905/conditions-inside-select-clause-in-linq">Here</a> is a related question with Linq-To-Objects.</p>
  
  <h3>EDIT</h3>
  
  <p>Before someone asks, I use <code>dynamic</code> in the example code to make it a bit easier and faster.</p>
  
  <h3>EDIT 2</h3>
  
  <p>My goal with this approach is, to only query the fields I need to improve performance. Check <a href="http://www.progware.org/Blog/post/Slow-Performance-Is-it-the-Entity-Framework-or-you.aspx" rel="nofollow noreferrer">http://www.progware.org/Blog/post/Slow-Performance-Is-it-the-Entity-Framework-or-you.aspx</a>.</p>
  
  <p>At the moment we use something like</p>
  
  <pre><code>static IEnumerable&lt;dynamic&gt; GetData(bool withchildren) {
      using (var model = new testEntities()) {
          var res = default(IQueryable&lt;dynamic&gt;);
          res = model.UserSet
              .Where(u =&gt; u.name != "")
              .ToList();
          return res;
      }
  }
  </code></pre>
  
  <p>And the performance is, according to Glimpse, horrible.</p>
  
  <h3>EDIT 3</h3>
  
  <p>Short side note, I made up some quick and dirty code. That is, why the <code>foreach</code> at the end was not needed. The actual code is not available at the moment.</p>