title: how to get count new message found as per enquiry/ticket with two way conditions
body: <p>i 'm working with crm application. i developed it finally i'm getting confused how i detect new messages as per flag must have display and message not have to true.</p>
  
  <p>here is my db schema for understanding problem.</p>
  
  <p><img src="https://i.stack.imgur.com/XQiRt.png" alt="enter image description here">
  here is my constructed query to get new message count :</p>
  
  <pre><code>int new_messages = 0;
                  foreach (var enquiry in db.Enquiries.Where(i =&gt; (i.ForwardTo.Equals(userid) || i.AttendBy.Equals(userid))).ToList())
                  {
                      if (enquiry != null)
                      {
                          bool IsIns = true;
                          foreach (var flag in db.Flags.Where(f =&gt; f.Enquiry_History_id.Equals(enquiry.Ref_no) &amp;&amp; f.User_id.Equals(userid)).Select(f =&gt; new { IsDisplay = f.IsDisplay }).ToList())
                          {
                              if (flag != null)
                              {
                                  if (flag.IsDisplay == false)
                                  {
                                      IsIns = false;
                                  }
                              }
                          }
                          if (IsIns == true)
                          {
                              foreach (var message in db.Messages.Where(m =&gt; m.Enquiry_History_id.Equals(enquiry.Ref_no) &amp;&amp; m.User_id.Equals(userid)).Select(m =&gt; new { IsRead = m.IsRead }).ToList())
                              {
                                  if (message.IsRead == true)
                                  {
                                      //do another stuff
                                  }
                                  else
                                  {
                                      new_messages++;
                                  }
                              }
                          }
                      }
                  }
                  lbl_msg.Text = new_messages.ToString();
  </code></pre>
  
  <p>here i have to take each enquiry that have been AttendBy or ForwardBy to user and check it first it should be display is not false and after that check as per each message should not have IsRead to true. i'm trying with my best way but this not gives me out put as i want.</p>