title: Ternary operator in LINQ where clause
body: <p>I am using linq for join multiple tables and write complex query. Here when i will have '0' as any parameter i.e. <code>CategoryId</code>, <code>GameId</code>, <code>LimitVariantId</code>, that means user has selected "All" from interface.</p>
  
  <p>My Sql query, when I will pass parameter values greater than '0' is : </p>
  
  <pre><code>select * from dbo.GameCombinations gc
  inner join dbo.StakeBuyInByStakeCategories sbsc
  on sbsc.StakeBuyInByStakeCategoryId = gc.StakeBuyInByStakeCategoryId
  inner join dbo.GameTables gt
  on gc.GameCombinationId = gt.GameCombinationId
  where gc.CurrencyId=1 and gc.GameTypeId=2
  and sbsc.StakeBuyInId=gt.BuyIn
  and gc.CategoryId=4
  and gc.GameId=7
  and gc.LimitVariantId=23
  and gc.StakeCategoryId in (3,5,6)
  </code></pre>
  
  <p>When I will pass CategoryId as <code>0</code> then My Sql query will be : </p>
  
  <pre><code>select * from dbo.GameCombinations gc
  inner join dbo.StakeBuyInByStakeCategories sbsc
  on sbsc.StakeBuyInByStakeCategoryId = gc.StakeBuyInByStakeCategoryId
  inner join dbo.GameTables gt
  on gc.GameCombinationId = gt.GameCombinationId
  where gc.CurrencyId=1 and gc.GameTypeId=2
  and sbsc.StakeBuyInId=gt.BuyIn
  --and gc.CategoryId=4
  and gc.GameId=7
  and gc.LimitVariantId=23
  and gc.StakeCategoryId in (3,5,6)
  </code></pre>
  
  <p>So I don't need to include that fields in where clause. For that I had written the following LINQ:</p>
  
  <pre><code>ProviderDB db = new ProviderDB();
  try
  {
      IQueryable&lt;dynamic&gt; query;
  
      if (StakeCategoryIdsByStakeBuyIn != null)
      {
          query = (from gc in db.GameCombinations.Where(x =&gt; x.CurrencyId == CurrencyId &amp;&amp; x.GameTypeId == GameTypeId
                              &amp;&amp; CategoryId &lt;= 0 ? true : x.CategoryId == CategoryId
                              &amp;&amp; GameId &lt;= 0 ? true : x.GameId == GameId
                              &amp;&amp; LimitVariantId &lt;= 0 ? true : x.LimitVariantId == LimitVariantId
                              &amp;&amp; StakeCategoryIdsByStakeBuyIn.Contains(x.StakeCategoryId)
                          )
                   join sbsc in db.StakeBuyInByStakeCategories
                   on gc.StakeBuyInByStakeCategoryId equals sbsc.StakeBuyInByStakeCategoryId
                   join gt in db.GameTables
                   on gc.GameCombinationId equals gt.GameCombinationId
                   join gx in db.Games
                   on gc.GameId equals gx.GameId into joined
                   from gx in joined.DefaultIfEmpty()
                   where gt.BuyIn == sbsc.StakeBuyInId
                   select new
                   {
                       GameTableId = gt.GameTableId,
                       Description = gt.Description,
                       BuyIn = gt.BuyIn,
                       Table = gx.GameName,
                       MaxAllowPlayer = gt.MaxAllowPlayer
                   }).Distinct();
      }
      else
      {
          query = (from gc in db.GameCombinations.Where(x =&gt; x.CurrencyId == CurrencyId &amp;&amp; x.GameTypeId == GameTypeId
                                &amp;&amp; CategoryId == 0 ? true : x.CategoryId == CategoryId
                                &amp;&amp; GameId == 0 ? true : x.GameId == GameId
                                &amp;&amp; LimitVariantId == 0 ? true : x.LimitVariantId == LimitVariantId
                                &amp;&amp; StakeCategoryIdsByStakeBuyIn == null
                          )
                   join sbsc in db.StakeBuyInByStakeCategories
                   on gc.StakeBuyInByStakeCategoryId equals sbsc.StakeBuyInByStakeCategoryId
                   join gt in db.GameTables
                   on gc.GameCombinationId equals gt.GameCombinationId
                   join sb in db.StakeBuyIns
                   on gt.BuyIn equals sb.StakeBuyInId
                   join gx in db.Games
                   on gc.GameId equals gx.GameId into joined
                   from gx in joined.DefaultIfEmpty()
                   where gt.BuyIn == sbsc.StakeBuyInId
                   select new
                   {
                       GameTableId = gt.GameTableId,
                       Description = gt.Description,
                       BuyIn = sb.StakeBuyInValue,
                       Table = gx.GameName,
                       MaxAllowPlayer = gt.MaxAllowPlayer
                   }).Distinct();
      }
  </code></pre>
  
  <p>But this will return all fields from my database. So Can any one help me to write these queries in LINQ with ternary condition that will return my filtered fields' records?</p>