title: Linq- preferable value in select
body: <p>If I have the following model:</p>
  
  <pre><code>Public class Person {
    public string Name {get; set;}
    public string Type {get; set;}
    public int Salary {get; set;}
  }
  </code></pre>
  
  <p>and I have a list of Persons, <code>List&lt;Person&gt; persons = new List&lt;Person&gt;();</code></p>
  
  <p>Now I want the most common salary of Managers, if none exists I want most common salary of MiddleManagers.</p>
  
  <p>I tried the following:</p>
  
  <pre><code>int salary= persons.Where(x =&gt; x.Type=="Manager" || x.Type=="MiddleManager")
                      .GroupBy(pr =&gt; pr.Salary)
                      .OrderByDescending(g =&gt; g.Count())
                      .Select(x =&gt; x.Key)
                      .FirstOrDefault();
  </code></pre>
  
  <p>but that would return the most common salary among both Managers and MiddleManagers, is there a way to achieve the above without going to:</p>
  
  <pre><code> int salary= persons.Where(x =&gt; x.Type=="Manager")
                          .GroupBy(pr =&gt; pr.Salary)
                          .OrderByDescending(g =&gt; g.Count())
                          .Select(x =&gt; x.Key)
                          .FirstOrDefault();
  if(salary==0)
   salary= persons.Where(x =&gt; x.Type=="MiddleManager")
                          .GroupBy(pr =&gt; pr.Salary)
                          .OrderByDescending(g =&gt; g.Count())
                          .Select(x =&gt; x.Key)
                          .FirstOrDefault();
  </code></pre>
  
  <p>Why would I want to avoid the above? because I have the same logic for many properties.</p>