title: How to avoid consecutive nested .Any in C# LINQ
body: <p>I'm having a Collection List BossList. In that I'm using nested .Any() to determine the condition. Now the Performance is Very slow in my real project. Consider the following sample source code.</p>
  
  <pre><code>void Main()
  {
  
      List&lt;Boss&gt; BossList = new List&lt;Boss&gt;()
      {
          new Boss()
          {
              ID = 101,
              Name = "Harry",
              Department = "Development",
              Gender = "Male",
              Role = "Manager",
              Employees = new List&lt;Person&gt;() {
                  new Person() {
                      ID = 101, 
                      SID = 102, 
                      Name = "Peter", 
                      Department = "Development", 
                      Gender = "Male", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;()
                      {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 8 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  },
                  new Person() {
                      ID = 101, 
                      SID = 103, 
                      Name = "Emma Watson", 
                      Department = "Development", 
                      Gender = "Female", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;() {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 5 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  },
  
              }
          },
          new Boss()
          {
              ID = 104,
              Name = "Raj",
              Department = "Development",
              Gender = "Male",
              Role = "Manager",
              Employees = new List&lt;Person&gt;()
              {
                  new Person() {
                      ID = 104, 
                      SID = 105, 
                      Name = "Kaliya", 
                      Department = "Development", 
                      Gender = "Male", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;() {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 8 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  },
                  new Person() {
                      ID = 104, 
                      SID = 103, 
                      Name = "Emma Watson", 
                      Department = "Development", 
                      Gender = "Female", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;() {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 5 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  },
  
              },
          },
          new Boss()
          {
              ID = 102,
              Name = "Peter",
              Department = "Development",
              Gender = "Male",
              Role = "Manager",
              Employees = new List&lt;Person&gt;()
              {
                  new Person() {
                      ID = 102, 
                      SID = 105, 
                      Name = "Kaliya", 
                      Department = "Development", 
                      Gender = "Male", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;() {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 6 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 8 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  },
                  new Person() {
                      ID = 102, 
                      SID = 103, 
                      Name = "Emma Watson", 
                      Department = "Development", 
                      Gender = "Female", 
                      Role = "Assistant",
                      PayInfo = new List&lt;PayrollInfo&gt;() {
                          new PayrollInfo() { Monthof2015 = 1, NetWorkingDays = 24, AbsentDays = 5 },
                          new PayrollInfo() { Monthof2015 = 2, NetWorkingDays = 23, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 3, NetWorkingDays = 20, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 4, NetWorkingDays = 22, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 5, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 6, NetWorkingDays = 26, AbsentDays = 9 },
                          new PayrollInfo() { Monthof2015 = 7, NetWorkingDays = 25, AbsentDays = 4 },
                          new PayrollInfo() { Monthof2015 = 8, NetWorkingDays = 21, AbsentDays = 3 },
                          new PayrollInfo() { Monthof2015 = 9, NetWorkingDays = 20, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 10, NetWorkingDays = 25, AbsentDays = 2 },
                          new PayrollInfo() { Monthof2015 = 11, NetWorkingDays = 24, AbsentDays = 1 },
                          new PayrollInfo() { Monthof2015 = 12, NetWorkingDays = 26, AbsentDays = 1 },
                      }
                  }
              }
          }
      };
  
      BossList.Where(i =&gt; i.Employees.Any(j =&gt; j.PayInfo.Any(s =&gt; s.AbsentDays &gt;6))).Select(m =&gt; m.Name).Dump();
  }
  </code></pre>
  
  <p>The Model Classes are</p>
  
  <pre><code>public class Person
  {
      public int ID { get; set; }
      public int SID { get; set; }
      public string Name { get; set; }
      public string Department { get; set; }
      public string Gender { get; set; }
      public string Role { get; set; }
      public List&lt;PayrollInfo&gt; PayInfo { get; set; }
  }
  
  public class Boss
  {
      public int ID { get; set; }
      public int SID { get; set; }
      public string Name { get; set; }
      public string Department { get; set; }
      public string Gender { get; set; }
      public string Role { get; set; }
      public List&lt;Person&gt; Employees { get; set; }
  }
  
  public class PayrollInfo
  {
      public int Monthof2015 { get; set; }
      public int NetWorkingDays { get; set; }
      public int AbsentDays { get; set; }
  }
  </code></pre>
  
  <p>The Main LINQ Query is</p>
  
  <pre><code>BossList.Where(i =&gt; i.Employees
                        .Any(j =&gt; j.PayInfo
                                   .Any(s =&gt; s.AbsentDays &gt;6)))
          .Select(m =&gt; m.Name);
  </code></pre>
  
  <p>In my main Project, it contains 6 nested .Any(). Here I can't able to create because of lengthy code.</p>
  
  <p>I'm using the query in EF as a IQueryable. The Database contains more than 1000K Records.</p>
  
  <p>Kindly suggest me, is there is any efficient way to maximize the performance ?</p>