Filtering units by a dynamic Lambda expression I have a simple if statement which, based on user input, determines which expression is going to be utilized. It looks like it can be done better.

Func&lt;Unit, bool&gt; exp;
if (onlyChildren)
    exp = u =&gt; u.PhaseId == id;
else
    exp = u =&gt; u.PhaseId != id;

var unitGroups = _db.Units.Where(exp).GroupBy(m =&gt; m.UnitTemplate, (m, group) =&gt; new UnitTemplateZoneVm()
{
    Id = m.Id,
    Units = group.ToList()
}).ToList();
Don't omit braces. Ever. It's such a bad idea (I learned this the hard way).

As far as your lambda, your method is not necessarily a bad thing, though there are always other ways to write it.

var unitGroups = _db.Units
                    .Where(u =&gt; (onlyChildren &amp;&amp; u.PhaseId == id) || (!onlyChildren &amp;&amp; u.PhaseId != id))
                    .GroupBy(m =&gt; .UnitTemplate, (m, group) =&gt; new UnitTemplateZoneVm()
                    {
                        Id = m.Id,
                        Units = group.ToList()
                    })
                    .ToList();


Whitespace is free, use it. It makes it clearer as to what steps belong together.

I would personally prefer this method, as you can extract this out into LINQ-SQL style, instead of using the methods. Personally, I prefer the LINQ-SQL style rather than the methods, but the choice is up to you.